You are on page 1of 6

Task 3

Student Name: Ayush Sharma UID: 20BCS6550


Branch: CSE- AIML Section/Group- PH20AIML2 - B
Semester: 4 Date of Performance: 19.05.2022
Subject Name: - ML Lab Subject Code: - 20CSF - 288

1. Aim/Overview of the practical:

Apply Linear Regression on Boston House Prediction


(sklearn dataset)

2. Theory:

 Linear Regression is the supervised Machine Learning model in


which the model finds the best fit linear line between the
independent and dependent variable i.e it finds the linear
relationship between the dependent and independent variable.

3. Code:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

Dataset = pd.read_csv('boston_test.csv')
Dataset.shape
Dataset.info()
Dataset=Dataset.drop_duplicates() #Dropping duplicates if any
Dataset.duplicated().sum()
Dataset.isna().sum()
plt.figure(figsize = (16, 10))
cor=Dataset.corr()
sns.heatmap(cor, annot = True)
plt.show()
X = Dataset[[ 'zn', 'indus', 'chas', 'nox', 'rm', 'age', 'dis', 'rad', 'tax',
'ptratio', 'black', 'lstat']]
y = Dataset['crim']

from sklearn.model_selection import train_test_split


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

from sklearn.linear_model import LinearRegression


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

predictions = regressor.predict(X_test)

plt.scatter(y_test,predictions)
plt.xlabel('Y Test')
plt.ylabel('Predicted Y')

from sklearn import metrics

print('MAE:', metrics.mean_absolute_error(y_test, predictions))


print('MSE:', metrics.mean_squared_error(y_test, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions)))

print(regressor.coef_)
print(regressor.intercept_)
4. Result :
 The MAE is : 3.86
 The MSE is : 81.88
 The RMSE is : 9.04
 The regressor coefficient and regressor intercept is :
[ 4.34350508e-02 -3.48056387e-02 -8.63696861e-01 -
5.18679986e-01
7.93531087e-01 -8.60382487e-03 -5.71435615e-01
6.39764564e-01
5.81821825e-04 1.01372745e-03 1.63484481e-02
3.16688040e-01]
-14.158918752334719

5. Graph :
6 . Learning Outcomes :

 Learned how to use linear regression in real datasets.


 Learned how to visually define the correlation btw datasets.
 Learned how to check null values and remove it .

Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like