for i in range(13):
plt.subplot(7,2,i 1) #7行2列第i 1個圖
plt.scatter(x_data[:,i],y_data,s=10) #橫縱坐標(biāo)和點的大小
plt.title(name_data[i])
plt.show()
print(name_data[i],np.corrcoef(x_data[:i]),y_data)
#打印刻畫每個維度特征與房價相關(guān)性的協(xié)方差矩陣
i_=[]
for i in range(len(y_data)):
if y_data[i] == 50:
i_.append(i)#存儲房價等于50 的異常值下標(biāo)
x_data = np.delete(x_data,i_,axis=0) #刪除樣本異常值數(shù)據(jù)
y_data = np.delete(y_data,i_,axis=0) #刪除標(biāo)簽異常值
name_data = dataset.feature_names
j_=[]
for i in range(13):
if name_data[i] =='RW'or name_data[i] == 'PTRATIO'or name_data[i] == 'LSTAT' : #提取'RM'、'PTRATIO'、'LSTAT'三個主要特征
continue
j_.append(i)#存儲其他次要特征下標(biāo)
x_data = np.delete(x_data,j_,axis=1)#在總特征中刪除次要特征
print(np.shape(y_data))
print(np.shape(x_data))
for i in range(len(y_data)):
plt.scatter(i,y_data[i],s=10)
#繪制真實值和預(yù)測值對比圖
def draw_infer_result(groud_truths,infer_results):
title='Boston'
plt.title(title, fontsize=24)
x = np.arange(-0.2,2)
y = x
plt.plot(x, y)
plt.xlabel('ground truth', fontsize=14)
plt.ylabel('infer result', fontsize=14)
plt.scatter(groud_truths, infer_results,color='green',label='training cost')
plt.grid()
plt.show()
draw_infer_result(y_test,lr_y_predict)
draw_infer_result(y_test,rr_y_predict)
draw_infer_result(y_test,lassr_y_predict)
draw_infer_result(y_test,svr_rbf_y_predict)
draw_infer_result(y_test,svr_lin_y_predict)
draw_infer_result(y_test,svr_poly_y_predict)
print("score of lr:",score_lr)
print("score of rr:",score_rr)
print("score of lassr:",score_lassr)
print("score of svr_rbf:",score_svr_rbf)
print("score of svr_lin:",score_svr_lin)
print("score of svr_poly:",score_svr_poly)