0% found this document useful (0 votes)
21 views

FML - Lab - Ipynb - Colab

Uploaded by

24im0002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

FML - Lab - Ipynb - Colab

Uploaded by

24im0002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8/11/24, 5:27 PM FML_Lab_2.

ipynb - Colab

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

cal_data = fetch_california_housing()

print(cal_data)
{'data': array([[ 8.3252 , 41. , 6.98412698, ..., 2.55555556,
37.88 , -122.23 ],
[ 8.3014 , 21. , 6.23813708, ..., 2.10984183,
37.86 , -122.22 ],
[ 7.2574 , 52. , 8.28813559, ..., 2.80225989,
37.85 , -122.24 ],
...,
[ 1.7 , 17. , 5.20554273, ..., 2.3256351 ,
39.43 , -121.22 ],
[ 1.8672 , 18. , 5.32951289, ..., 2.12320917,
39.43 , -121.32 ],
[ 2.3886 , 16. , 5.25471698, ..., 2.61698113,
39.37 , -121.24 ]]), 'target': array([4.526, 3.585, 3.521, ..., 0.923, 0.847, 0.894]), 'frame': None, 'target_na

x = cal_data.data

print(x)
[[ 8.3252 41. 6.98412698 ... 2.55555556
37.88 -122.23 ]
[ 8.3014 21. 6.23813708 ... 2.10984183
37.86 -122.22 ]
[ 7.2574 52. 8.28813559 ... 2.80225989
37.85 -122.24 ]
...
[ 1.7 17. 5.20554273 ... 2.3256351
39.43 -121.22 ]
[ 1.8672 18. 5.32951289 ... 2.12320917
39.43 -121.32 ]
[ 2.3886 16. 5.25471698 ... 2.61698113
39.37 -121.24 ]]

y is here target variable

y = cal_data.target

print(y)
[4.526 3.585 3.521 ... 0.923 0.847 0.894]

20640 data and 8 features..features means what is the size of the house...and so on...

print(x.shape)
(20640, 8)

Double-click (or enter) to edit

y.shape here is 1d array..thats why here outcome is only one

print(y.shape)
(20640,)

Double-click (or enter) to edit

https://colab.research.google.com/drive/1xxGAcMG1eU-HeNgPNOWY0tQzxzdR30wV 1/3
8/11/24, 5:27 PM FML_Lab_2.ipynb - Colab

print(x[100,:])
[ 2.4912e+00 2.9000e+01 3.7248e+00 1.1312e+00 2.3040e+03 1.8432e+00
3.7810e+01 -1.2225e+02]

Splitting

x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = .2, random_state = 42)

training..its creating an object for linear regression..Now we will fit the data into objeect. first create an object then fit the particular object.

model = LinearRegression()

Double-click (or enter) to edit

for fitting data

model.fit(x_train, y_train)

▾ LinearRegression
LinearRegression()

now we have to predict unseen data..for x_test

y_pred = model.predict(x_test)

mse = mean_squared_error(y_test, y_pred)

print(mse)
0.5558915986952422

use xtrainnew to xtrain with only one feature..

x_train_new = x_train[:,0]

x_train_new = np.reshape(x_train_new, (x_train_new.shape[0],1))

model1 = LinearRegression()

model1.fit(x_train_new, y_train)

▾ LinearRegression
LinearRegression()

print(x_train_new)
[[3.2596]
[3.8125]
[4.1563]
...
[2.9344]
[5.7192]
[2.5755]]

y_pred_new = x_test[:,0]

y_pred_new = model1.predict(x_test_new)
mse1 = mean_squared_error(y_test, y_pred_new)
print(mse1)

https://colab.research.google.com/drive/1xxGAcMG1eU-HeNgPNOWY0tQzxzdR30wV 2/3
8/11/24, 5:27 PM FML_Lab_2.ipynb - Colab

0.7091157771765549

plot using matplotlib.. line, scatter.. plot x,y== scatterplot

plt.scatter(x_test_new[:100,],y_test[:100,])
plt.plot(x_test_new[:100,], y_pred_new[:100,])
plt.show()

Double-click (or enter) to edit

https://colab.research.google.com/drive/1xxGAcMG1eU-HeNgPNOWY0tQzxzdR30wV 3/3

You might also like