0% found this document useful (0 votes)
16 views4 pages

ML Assignment1

Uploaded by

Ak Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

ML Assignment1

Uploaded by

Ak Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

2/11/24, 6:25 linear regression.

ipynb - Colaboratory
Anil Kumar
(0901CS223D04)

import numpy as np
from sklearn.datasets import
fetch_california_housingimport matplotlib.pyplot as
plt
from pandas.plotting import
scatter_matrixfrom sklearn.metrics
import r2_score
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.metrics import mean_squared_error

housing = fetch_california_housing(as_frame=True)
housing = housing.frame
housing.head()

Medlnc HouseAge AveRooms AveBedrms Population Aveoccup Latitude Longitude MedHouseVal


0 8.3252 41.0 6.984127 1.023810 322.0 2.555556 37.88 -122.23 4.526
1 8.3014 21.0 6.238137 0.971880 2401.0 2.109842 37.86 -122.22 3.585
2 7.2574 52.0 8.288136 1.073446 496.0 2.802260 37.85 -122.24 3.521
3 5.6431 52.0 5.817352 1.073059 558.0 2.547945 37.85 -122.25 3.413
4 3.8462 52.0 6.281853 1.081081 565.0 2.181467 37.85 -122.25 3.422

housing.hist(bins=50, figsize=(12,8))
plt.show()

1/4
2/11/24, 6:25 PM linear regression.ipynb - Colaboratory
housing.plot(kind=”scatter”, x=”Longitude”,y=“Latitude”, c=“MedHouseVal“, cmap=“jet”, colorbar=True, legend=True, splt.show()

Longitude

attributes = ['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'Aveoccup', 'MedHouseVal']


scatter_matrix(housing[attributes], figsize=(12,8))
plt.show()

2/4
2/11/24, 6:25 PM linear regression.ipynb - Colaboratory

corr = housing.corr()
corr['MedHouseVal'].sort_values(ascending=True)

Latitude -0.144160
AveBedrms -0.046701
Longitude -0.045967
Population -0.024650
Ave0ccup -0.023737
HouseAge 0.105623
AveRooms 0.151948
MedInc 0.688075
MedHouseVal 1.000000
Name: MedHouseVal, dtype: float64

X = housing.iloc[:,:-1]
y = housing.iloc[:,-1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

regression_pipeline = Pipeline([ ('scaler',


StandardScaler()), ('regressor',
LinearRegression())

regression_pipeline.fit(X_train,y_train)

””””Pipeline”””’

• StandardScaler

• L1nearR
gresston

y_pred = regression_pipeline.predict(X_test)
r2_score( y_test, y_pred)

0.575787706032451

3/4
2/11/24, 6:25 PM linear regression.ipynb - Colaboratory

print(y test.dtype)
print(y pred.dtype)

float64float64

plt.scatter(y pred, y test)


plt.xlabel("Predicted Values")
plt.ylabel("Actual Values“) plt.show()
Actual Values

4/4

You might also like