You are on page 1of 3

Course Name: Deep Learning Lab Course Code: CSA-412

Experiment No. 2

AIM: Write a program to implement Linear Regression Implementation on any dataset.

SOFTWARE: Google Colab.

DESCRIPTION: In Machine Learning lingo, Linear Regression (LR) means simply


finding the best fitting line that explains the variability between the dependent and
independent features very well.

STEPS:
Let’s try to implement the linear regression in both scikit-learn and statsmodel.

Scikit-learn Statsmodels
Intercept Includes intercept by default. We need to add the intercept.

Model The score method in scikit-learn gives It shows many statistical results like
Evaluation us the accuracy of the model p-value, F-test, Confidential Interval
Regularization It uses “L2” by default; We can also set It does not use any regularization
the parameter to “None” if we want. parameter.
Advantage It has a lot of parameters and is easy to It has a lot of parameters and is easy to
use. use.

DATASET: x = np.random.rand(50,1)
y = 3+3 * x + np.random.rand(50,1)

IMPLEMENTATION:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(10)

x = np.random.rand(50, 1)
y = 3 + 3 * x + np.random.rand(50, 1)

Name:TANISHQ UID:19BCS2158
Course Name: Deep Learning Lab Course Code: CSA-412

plt.scatter(x,y,s=10)

plt.xlabel('x_dummy')
plt.ylabel('y_dummy')
plt.show()

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()

regressor.fit(x, y)

pred = regressor.predict(x)

from sklearn.metrics import r2_score, mean_squared_error


mse = mean_squared_error(y, pred)
r2 = r2_score(y, pred)#Best fit lineplt.scatter(x, y)
plt.plot(x, pred, color = 'Black', marker = 'o')

print("Mean Squared Error : ", mse)


print("R-Squared :" , r2)
print("Y-intercept :" , regressor.intercept_)
print("Slope :" , regressor.coef_)

R-Squared : 0.9068822972556425
Y-intercept : [3.41354381]
Slope : [[3.11024701]]
Implementation of Linear Regression using Statsmodel,

import statsmodels.api as sm

X = sm.add_constant(x)

result = sm.OLS(y, x).fit()

print(result.summary())

Name:TANISHQ UID:19BCS2158
Course Name: Deep Learning Lab Course Code: CSA-412

OUTPUT:

Name:TANISHQ UID:19BCS2158

You might also like