学习笔记-波士顿房价预测--KNN Regression

#学习笔记

波士顿房价预测–KNN Regression

#!/usr/bin/env python

-- coding: utf-8 --

author = ‘Administrator’
from sklearn.datasets import load_boston
boston = load_boston()

print(boston.DESCR)

from sklearn.model_selection import train_test_split
import numpy as np
X_train,X_test,y_train,y_test = train_test_split(boston.data,boston.target,test_size=0.25,random_state=33)
print('The max target value is ',np.max(boston.target))
print('The min target value is ',np.min(boston.target))
print('The average target value is ',np.mean(boston.target))
from sklearn.preprocessing import StandardScaler
ss_X = StandardScaler()
ss_y = StandardScaler()
X_train= ss_X.fit_transform(X_train)
X_test = ss_X.transform(X_test)
y_train=ss_y.fit_transform(y_train.reshape(-1,1))
y_test=ss_y.transform(y_test.reshape(-1,1))
from sklearn.neighbors import KNeighborsRegressor
uni_knr = KNeighborsRegressor(weights=‘uniform’)
uni_knr.fit(X_train,y_train)
uni_knr_y_predict= uni_knr.predict(X_test)
dis_knr = KNeighborsRegressor(weights=‘distance’)
dis_knr.fit(X_train,y_train)
dis_knr_y_predict = dis_knr.predict(X_test)
#1.预测方式为平均回归的KNR
from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error
print (‘R-squared value of uniform-weighted KNR is’,uni_knr.score(X_test,y_test))
print (‘the MSE of uniform-weighted KNR is’,mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(uni_knr_y_predict)))
print (‘the MAE of uniform-weighted KNR is’,mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(uni_knr_y_predict)))
#2.预测方式为根据加权距离的KNR
print (‘R-squared value of distance-weighted KNR is’,dis_knr.score(X_test,y_test))
print (‘the MSE of uniform-weighted KNR is’,mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(dis_knr_y_predict)))
print (‘the MAE of uniform-weighted KNR is’,mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(dis_knr_y_predict)))
学习笔记-波士顿房价预测--KNN Regression

import matplotlib as mpl
import matplotlib.pyplot as plt

设置字符集防止乱码

mpl.rcParams[‘font.sans-serif’] = [u’simHei’]
mpl.rcParams[‘axes.unicode_minus’] = False
titles=[‘uniform’,‘distance’]
colors=[‘g-’,‘b-’]
plt.figure(figsize=(16,8),facecolor=‘w’)
In_x_test = range(len(X_test))
plt.plot(In_x_test,y_test,‘r-’,lw=2,label=u’真实值’)

#画图
plt.plot(In_x_test,uni_knr_y_predict,label=u’%s,R2R^2=%.3f’%(titles[0],uni_knr.score(X_test,y_test)))
plt.plot(In_x_test,dis_knr_y_predict,label=u’%s,R2R^2=%.3f’%(titles[1],dis_knr.score(X_test,y_test)))
#图形显示
plt.legend(loc=‘upper left’)
plt.grid(True)
plt.title(u’波士顿房屋价格预测’)
plt.show()
学习笔记-波士顿房价预测--KNN Regression
#参考
jho9o5的博客