K近邻回归模型对Boston房价进行预测,同时对性能进行评估(1.使用普通的算术平均法2.考虑距离差异进行加权平均)

from sklearn.datasets import load_boston
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error
#导入数据
boston = load_boston()
#查看数据信息print(boston.DESCR)
X = boston.data
y = boston.target 
#对数据进行分割
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=33)
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)
y_test = ss_y.transform(y_test)
#K近邻(回归)1.使用普通的算术平均法2.考虑距离差异进行加权平均
#初始化K近邻回归器,使得预测的方式为平均回归:weights='uniform'
uni_knr = KNeighborsRegressor(weights='uniform')
uni_knr.fit(X_train,y_train)
uni_knr_y_predict = uni_knr.predict(X_test)
#初始化K近邻回归器,调整配置使得预测方式为距离加权回归weights='distance'
dis_knr = KNeighborsRegressor(weights='distance')
dis_knr.fit(X_train,y_train)
dis_knr_y_predict=dis_knr.predict(X_test)
#使用R-squared,MSE以及MAE三种指标对平均回归配置的K近邻模型在测试集上进行性能评估
print('R-squared value of uniform-weight KNeighorRegression:',uni_knr.score(X_test,y_test))
print('\n'*2)
print('The mean squared error of uniform-weighted KNeighorRegression: ',mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(uni_knr_y_predict)))
print('\n'*2)
print('The mean absolute error of uniform-weighted KNeighborRegression',mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(uni_knr_y_predict)))
#使用R-squared,MSE以及MAE三种指标对距离加权回归配置的K近邻模型在测试集上进行性能评估
print('R-squared value of diatance-weight KNeighorRegression:',dis_knr.score(X_test,y_test))
print('\n'*2)
print('The mean squared error of diatance-weighted KNeighorRegression: ',mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(dis_knr_y_predict)))
print('\n'*2)
print('The mean absolute error of diatance-weighted KNeighborRegression',mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(dis_knr_y_predict)))

你可能感兴趣的:(机器学习)