FML - Lab - Ipynb - Colab
FML - Lab - Ipynb - Colab
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 = 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)
print(y.shape)
(20640,)
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
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()
model.fit(x_train, y_train)
▾ LinearRegression
LinearRegression()
y_pred = model.predict(x_test)
print(mse)
0.5558915986952422
x_train_new = x_train[:,0]
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
plt.scatter(x_test_new[:100,],y_test[:100,])
plt.plot(x_test_new[:100,], y_pred_new[:100,])
plt.show()
https://colab.research.google.com/drive/1xxGAcMG1eU-HeNgPNOWY0tQzxzdR30wV 3/3