import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from matplotlib.font_manager import FontProperties from sklearn.linear_model import LinearRegression %matplotlib inline font = FontProperties(fname='/Library/Fonts/Heiti.ttc')
housing-data.txt
文件能夠加我微信獲取:a1171958281算法
df = pd.read_csv('housing-data.txt', sep='\s+', header=0) 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 |
使用sns庫的pairplot()方法繪製的散點圖矩陣能夠查看數據集內部特徵之間的關係,例如能夠觀察到特徵間分佈關係以及離羣樣本。
本文只繪製了三列(RM、MEDV(標記)、LSTAT)特徵和標記之間的聯繫,有興趣的能夠調用該方法查看其它特徵之間的關係。微信
# 選擇三列特徵 cols = ['RM', 'MEDV', 'LSTAT'] # 構造三列特徵之間的聯繫即構造散點圖矩陣 sns.pairplot(df[cols], height=3) plt.tight_layout() plt.show()
上圖能夠看出第一行(RM)第二列(MEDV)的特徵與標記存在線性關係;第二行(MEDV)第二列(MEDV)即MEDV值可能呈正態分佈。性能
使用sns.heatmap()方法繪製的關聯矩陣能夠看出特徵之間的相關性大小,關聯矩陣是包含皮爾森積矩相關係數的正方形矩陣,用來度量特徵對之間的線性依賴關係。3d
# 求解上述三列特徵的相關係數 ''' 對於通常的矩陣X,執行A=corrcoef(X)後,A中每一個值的所在行a和列b,反應的是原矩陣X中相應的第a個列向量和第b個列向量的 類似程度(即相關係數) ''' cm = np.corrcoef(df[cols].values.T) # 控制顏色刻度即顏色深淺 sns.set(font_scale=2) # 構造關聯矩陣 hm = sns.heatmap(cm, cbar=True, annot=True, square=True, fmt='.2f', annot_kws={ 'size': 20}, yticklabels=cols, xticklabels=cols) plt.show()
上圖能夠看出特徵LSTAT和標記MEDV的具備最高的相關性-0.74,可是在散點圖矩陣中會發現LSTAT和MEDV之間存在着明顯的非線性關係;而特徵RM和標記MEDV也具備較高的相關性0.70,而且從散點矩陣中會發現特徵RM和標記MEDV之間存在着線性關係。所以接下來將使用RM做爲線性迴歸模型的特徵。code
X = df[['RM']].values y = df['MEDV'].values lr = LinearRegression() lr.fit(X, y)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
plt.scatter(X, y, c='r', s=30, edgecolor='white',label='訓練數據') plt.plot(X, lr.predict(X), c='g') plt.xlabel('平均房間數目[MEDV]', fontproperties=font) plt.ylabel('以1000美圓爲計價單位的房價[RM]', fontproperties=font) plt.title('波士頓房價預測', fontproperties=font, fontsize=20) plt.legend(prop=font) plt.show() print('普通線性迴歸斜率:{}'.format(lr.coef_[0]))
普通線性迴歸斜率:9.10210898118031
使用RANSAC算法以後能夠發現線性迴歸擬合的線與未用RANSAC算法擬合出來的線的斜率不一樣,能夠說RANSAC算法下降了離羣值潛在的影響,可是這並不能說明這種方法對將來新數據的預測性能是否有良性影響。orm