You are on page 1of 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment-2.2
Student Name: Vishal Kumar Singh UID:21BCS2439
Branch:CSE Section/Group:702/B
Semester:5th Date Of Performance:
SubjectName:AIML LAB SubjectCode: 21CSH-316

1. Aim: Implementing Linear Regression and Logistic Regression models

2. Input/Apparatus Used: Google Collab

3. Procedure/Algorithm:

Linear Regression: Linear Regression is a supervised machine learning


model that establishes a linear relationship between independent variables
and a continuous target variable. It aims to find the best-fitting line to predict
numerical outcomes, such as predicting house prices based on features like
square footage and number of bedrooms.

Logistic Regression: Logistic Regression is a supervised machine learning


model for binary classification. It uses the logistic (sigmoid) function to
estimate the probability of an input belonging to one of two classes (0 or 1).
It's commonly used for tasks like spam detection and medical diagnosis.

4. Code:

Linear Regression:

pip install scikit-learn

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Generate some sample data


np.random.seed(0)
np.random.rand(100, 1)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
y = 4 + 3 * X + np.random.randn(100, 1)

# Split the data into training and test sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

# Create and train a Linear Regression model


lr_model = LinearRegression()
lr_model.fit(X_train, y_train)

# Make predictions on the test data


y_pred = lr_model.predict(X_test)

# Calculate the Mean Squared Error


mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")

Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Logistic Regression:

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Generate some sample data


np.random.seed(0)
X = 2 * np.random.rand(100, 2)
y = (2 * X[:, 0] + 3 * X[:, 1] + np.random.randn(100)) > 2

# Split the data into training and test sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

# Create and train a Logistic Regression model


logistic_model = LogisticRegression()
logistic_model.fit(X_train, y_train)

# Make predictions on the test data


y_pred = logistic_model.predict(X_test)

# Calculate the accuracy


accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

You might also like