使用两种不同配置的K近邻(回归)模型对美国波士顿房价数据进行回归预测

对美国波士顿地区房价数据进行预处理

# 从sklearn.datasets导入波士顿房价数据读取器
from sklearn.datasets import load_boston
# 从读取房价数据存储在变量boston中
boston = load_boston()
# 输出数据描述
print boston.DESCR
# 从sklearn.model导入数据分割器
from sklearn.model_selection import train_test_split
# 导入numpy并重命名为np
import numpy as np

X = boston.data
y = boston.target

# 随机采样25%的数据构建测试样本,其余作为训练样本
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=0.25)

# 分析回归目标值的差异
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)

# 从sklearn.preprocessing导入数据标准化模块
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)
y_test = ss_y.transform(y_test)

使用两种不同配置的K近邻回归模型对美国波士顿房价数据进行回归预测


# 从sklearn.neighbors导入KNeighborRegressor(K近邻回归器)
from sklearn.neighbors import KNeighborsRegressor
# 初始化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)

对两种不同配置的K近邻回归模型在美国波士顿房价数据上进行预测性能的评估

from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
# 使用R-squared、MSE以及MAE三种指标对平均回归配置的K近邻模型在测试集上进行性能评估。
print 'R-squared value of uniform-weighted KNeighorRegression:', uni_knr.score(X_test, y_test)
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 'The mean absoluate error of uniform-weighted KNeighorRegression', 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 distance-weighted KNeighorRegression:', dis_knr.score(X_test, y_test)
print 'The mean squared error of distance-weighted KNeighorRegression:', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(dis_knr_y_predict))
print 'The mean absoluate error of distance-weighted KNeighorRegression:', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(dis_knr_y_predict))

输出结果

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