房屋数据
特征:
CRIM:城镇人均犯罪率
ZN:占地面积超过25 000平方英尺的住宅用地比例
INDUS:城镇非零售营业面积占比
CHAS:查尔斯河哑变量(如果临河有大片土地为1,否则为0)
NOX:一氧化氮浓度(千万分之一)
RM:平均每户的房间数
AGE:1940以前建造的自用房单位比例
DIS:波士顿五个就业中心的加权距离
RAD:辐射可达的公路的索引
TAX:$10 000—每10 000美元全额财产的税率
PTRATIO:城镇师生比例
B:1000人中非裔美国人的比例
LSTAT:地位较低人口的百分比
目标变量:
MEDV:自住房的中位价(千美元)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df=pd.read_csv('housing-Copy1.data.txt',header=None,sep='\s+')
df.columns=['CRIM','ZN','INDUS','CHAS',
'NOX','RM','AGE','DIS',
'RAD','TAX','PTRATIO','B','LSTAT','MEDV']
df.head()
CRIM | ZN | INDUS | CHAS | NOX | RM | AGE | DIS | RAD | TAX | PTRATIO | B | LSTAT | MEDV | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.00632 | 18.0 | 2.31 | 0 | 0.538 | 6.575 | 65.2 | 4.0900 | 1 | 296.0 | 15.3 | 396.90 | 4.98 | 24.0 |
1 | 0.02731 | 0.0 | 7.07 | 0 | 0.469 | 6.421 | 78.9 | 4.9671 | 2 | 242.0 | 17.8 | 396.90 | 9.14 | 21.6 |
2 | 0.02729 | 0.0 | 7.07 | 0 | 0.469 | 7.185 | 61.1 | 4.9671 | 2 | 242.0 | 17.8 | 392.83 | 4.03 | 34.7 |
3 | 0.03237 | 0.0 | 2.18 | 0 | 0.458 | 6.998 | 45.8 | 6.0622 | 3 | 222.0 | 18.7 | 394.63 | 2.94 | 33.4 |
4 | 0.06905 | 0.0 | 2.18 | 0 | 0.458 | 7.147 | 54.2 | 6.0622 | 3 | 222.0 | 18.7 | 396.90 | 5.33 | 36.2 |
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 506 entries, 0 to 505
Data columns (total 14 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 CRIM 506 non-null float64
1 ZN 506 non-null float64
2 INDUS 506 non-null float64
3 CHAS 506 non-null int64
4 NOX 506 non-null float64
5 RM 506 non-null float64
6 AGE 506 non-null float64
7 DIS 506 non-null float64
8 RAD 506 non-null int64
9 TAX 506 non-null float64
10 PTRATIO 506 non-null float64
11 B 506 non-null float64
12 LSTAT 506 non-null float64
13 MEDV 506 non-null float64
dtypes: float64(12), int64(2)
memory usage: 55.5 KB
此方法打印有关 DataFrame 的信息,包括索引 dtype 和列、非 null 值和内存使用情况。
1. 可视化
#对角线柱状图
col=['LSTAT','INDUS','NOX','RM','MEDV']
sns.pairplot(df[col],height=2.5)
plt.tight_layout()#调整子图之间和周围的填充物
plt.show()
#对角线密度图(kde)
col=['LSTAT','INDUS','NOX','RM','MEDV']
sns.pairplot(df[col],height=2.5,diag_kind='kde')
plt.tight_layout()#调整子图之间和周围的填充物
plt.show()
RM 和 MEDV呈现线性关系较明显
sns.pairplot(df,height=3,diag_kind='hist')
plt.tight_layout()
plt.show()
cm=np.corrcoef(df[col].values.T)
cm
array([[ 1. , 0.60379972, 0.59087892, -0.61380827, -0.73766273],
[ 0.60379972, 1. , 0.76365145, -0.39167585, -0.48372516],
[ 0.59087892, 0.76365145, 1. , -0.30218819, -0.42732077],
[-0.61380827, -0.39167585, -0.30218819, 1. , 0.69535995],
[-0.73766273, -0.48372516, -0.42732077, 0.69535995, 1. ]])
sns.set_theme(font_scale=1.5)
hm=sns.heatmap(cm,
cbar=True,
annot=True,
square=True,
fmt='.2f',
annot_kws={
'size':15},
yticklabels=col,
xticklabels=col)
plt.show()
cm1=np.corrcoef(df[df.columns].values.T)
plt.figure(figsize=(10,10))
sns.set_theme(font_scale=1.5)
hm=sns.heatmap(cm1,
cbar=True,
annot=True,
square=True,
fmt='.2f',
annot_kws={
'size':15},
yticklabels=df.columns,
xticklabels=df.columns,
cmap='Blues')
plt.show()
2. 梯度下降实现回归参数求解
class LinearRegressionGD(object):
def __init__(self,eta=0.001,n_iter=50):
self.eta=eta
self.n_iter=n_iter
def fit(self,X,y):
self.w_=np.zeros(1+X.shape[1])
self.cost_=[]
for i in range(self.n_iter):
output=self.net_input(X)
errors=(y-output)
self.w_[1:]+=self.eta*X.T.dot(errors)
self.w_[0]=self.eta*errors.sum()
cost=(errors**2).sum()/2.0
self.cost_.append(cost)
return self
def net_input(self,X):
return np.dot(X,self.w_[1:])+self.w_[0]
def predict(self,X):
return self.net_input(X)