Prg7a - Jupyter Notebook
Prg7a - Jupyter Notebook
ipynb#
In [4]: data.head()
Out[4]:
CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTAT MEDV
0 0.00632 18.0 2.31 0.0 0.538 6.575 65.2 4.0900 1 296 15.3 396.90 4.98 24.0
1 0.02731 0.0 7.07 0.0 0.469 6.421 78.9 4.9671 2 242 17.8 396.90 9.14 21.6
2 0.02729 0.0 7.07 0.0 0.469 7.185 61.1 4.9671 2 242 17.8 392.83 4.03 34.7
3 0.03237 0.0 2.18 0.0 0.458 6.998 45.8 6.0622 3 222 18.7 394.63 2.94 33.4
4 0.06905 0.0 2.18 0.0 0.458 7.147 54.2 6.0622 3 222 18.7 396.90 NaN 36.2
In [6]: data.shape
1 of 12 15/04/25, 12:58
prg7a - Jupyter Notebook http://localhost:8888/notebooks/4AD22CG039/prg7a.ipynb#
In [8]: data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 506 entries, 0 to 505
Data columns (total 14 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 CRIM 486 non-null float64
1 ZN 486 non-null float64
2 INDUS 486 non-null float64
3 CHAS 486 non-null float64
4 NOX 506 non-null float64
5 RM 506 non-null float64
6 AGE 486 non-null float64
7 DIS 506 non-null float64
8 RAD 506 non-null int64
9 TAX 506 non-null int64
10 PTRATIO 506 non-null float64
11 B 506 non-null float64
12 LSTAT 486 non-null float64
13 MEDV 506 non-null float64
dtypes: float64(12), int64(2)
memory usage: 55.5 KB
2 of 12 15/04/25, 12:58
prg7a - Jupyter Notebook http://localhost:8888/notebooks/4AD22CG039/prg7a.ipynb#
In [9]: data.nunique()
In [10]: data.CHAS.unique()
In [12]: data.ZN.unique()
Out[12]: array([ 18. , 0. , 12.5, 75. , 21. , 90. , 85. , 100. , 25. ,
17.5, 80. , nan, 28. , 45. , 60. , 95. , 82.5, 30. ,
22. , 20. , 40. , 55. , 52.5, 70. , 34. , 33. , 35. ])
3 of 12 15/04/25, 12:58
prg7a - Jupyter Notebook http://localhost:8888/notebooks/4AD22CG039/prg7a.ipynb#
In [14]: data.isnull().sum()
Out[14]: CRIM 20
ZN 20
INDUS 20
CHAS 20
NOX 0
RM 0
AGE 20
DIS 0
RAD 0
TAX 0
PTRATIO 0
B 0
LSTAT 20
MEDV 0
dtype: int64
In [15]: data.duplicated().sum()
Out[15]: 0
4 of 12 15/04/25, 12:58
prg7a - Jupyter Notebook http://localhost:8888/notebooks/4AD22CG039/prg7a.ipynb#
In [20]: df.isnull().sum()
Out[20]: CRIM 0
ZN 0
INDUS 0
CHAS 0
NOX 0
RM 0
AGE 0
DIS 0
RAD 0
TAX 0
PTRATIO 0
B 0
LSTAT 0
MEDV 0
dtype: int64
In [21]: df.head()
Out[21]:
CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTAT MEDV
0 0.00632 18.0 2.31 0.0 0.538 6.575 65.2 4.0900 1 296 15.3 396.90 4.98 24.0
1 0.02731 0.0 7.07 0.0 0.469 6.421 78.9 4.9671 2 242 17.8 396.90 9.14 21.6
2 0.02729 0.0 7.07 0.0 0.469 7.185 61.1 4.9671 2 242 17.8 392.83 4.03 34.7
3 0.03237 0.0 2.18 0.0 0.458 6.998 45.8 6.0622 3 222 18.7 394.63 2.94 33.4
4 0.06905 0.0 2.18 0.0 0.458 7.147 54.2 6.0622 3 222 18.7 396.90 11.43 36.2
In [24]: df['CHAS']=df['CHAS'].astype('int')
5 of 12 15/04/25, 12:58
prg7a - Jupyter Notebook http://localhost:8888/notebooks/4AD22CG039/prg7a.ipynb#
In [25]: df.describe().T
Out[25]:
count mean std min 25% 50% 75% max
6 of 12 15/04/25, 12:58
prg7a - Jupyter Notebook http://localhost:8888/notebooks/4AD22CG039/prg7a.ipynb#
7 of 12 15/04/25, 12:58
prg7a - Jupyter Notebook http://localhost:8888/notebooks/4AD22CG039/prg7a.ipynb#
In [28]: corr=df.corr(method='pearson')
plt.figure(figsize=(10,8))
sns.heatmap(corr, annot=True, cmap="coolwarm", fmt=".2f", linewidth=0.5)
plt.xticks(rotation=90, ha='right')
plt.yticks(rotation=0)
plt.title("Correlation Matrix Heatmap")
plt.show()
8 of 12 15/04/25, 12:58
prg7a - Jupyter Notebook http://localhost:8888/notebooks/4AD22CG039/prg7a.ipynb#
9 of 12 15/04/25, 12:58
prg7a - Jupyter Notebook http://localhost:8888/notebooks/4AD22CG039/prg7a.ipynb#
In [35]: x=df.drop('MEDV',axis=1)
y=df['MEDV']
In [36]: scale=StandardScaler()
x_scaled=scale.fit_transform(X)
In [37]:
x_train, x_test, y_train, y_test = train_test_split(x_scaled, y, test_size=0.2, random_state=42)
In [38]: model=LinearRegression()
model.fit(x_train,y_train)
Out[38]: ▾ LinearRegression
LinearRegression()
10 of 12 15/04/25, 12:58
prg7a - Jupyter Notebook http://localhost:8888/notebooks/4AD22CG039/prg7a.ipynb#
11 of 12 15/04/25, 12:58
prg7a - Jupyter Notebook http://localhost:8888/notebooks/4AD22CG039/prg7a.ipynb#
In [ ]:
12 of 12 15/04/25, 12:58