python3 学习机器学习api
使用两种k近邻回归模型 分别是 平均k近邻回归 和 距离加权k近邻回归 进行预测
git: https://github.com/linyi0604/MachineLearning
代码:
1 from sklearn.datasets import load_boston 2 from sklearn.cross_validation import train_test_split 3 from sklearn.preprocessing import StandardScaler 4 from sklearn.neighbors import KNeighborsRegressor 5 from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error 6 import numpy as np 7 8 # 1 准备数据 9 # 读取波士顿地区房价信息 10 boston = load_boston() 11 # 查看数据描述 12 # print(boston.DESCR) # 共506条波士顿地区房价信息,每条13项数值特征描述和目标房价 13 # 查看数据的差异情况 14 # print("最大房价:", np.max(boston.target)) # 50 15 # print("最小房价:",np.min(boston.target)) # 5 16 # print("平均房价:", np.mean(boston.target)) # 22.532806324110677 17 18 x = boston.data 19 y = boston.target 20 21 # 2 分割训练数据和测试数据 22 # 随机采样25%作为测试 75%作为训练 23 x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=33) 24 25 26 # 3 训练数据和测试数据进行标准化处理 27 ss_x = StandardScaler() 28 x_train = ss_x.fit_transform(x_train) 29 x_test = ss_x.transform(x_test) 30 31 ss_y = StandardScaler() 32 y_train = ss_y.fit_transform(y_train.reshape(-1, 1)) 33 y_test = ss_y.transform(y_test.reshape(-1, 1)) 34 35 # 4 两种k近邻回归行学习和预测 36 # 初始化k近邻回归模型 使用平均回归进行预测 37 uni_knr = KNeighborsRegressor(weights="uniform") 38 # 训练 39 uni_knr.fit(x_train, y_train) 40 # 预测 保存预测结果 41 uni_knr_y_predict = uni_knr.predict(x_test) 42 43 # 多初始化k近邻回归模型 使用距离加权回归 44 dis_knr = KNeighborsRegressor(weights="distance") 45 # 训练 46 dis_knr.fit(x_train, y_train) 47 # 预测 保存预测结果 48 dis_knr_y_predict = dis_knr.predict(x_test) 49 50 # 5 模型评估 51 # 平均k近邻回归 模型评估 52 print("平均k近邻回归的默认评估值为:", uni_knr.score(x_test, y_test)) 53 print("平均k近邻回归的R_squared值为:", r2_score(y_test, uni_knr_y_predict)) 54 print("平均k近邻回归的均方误差为:", mean_squared_error(ss_y.inverse_transform(y_test), 55 ss_y.inverse_transform(uni_knr_y_predict))) 56 print("平均k近邻回归 的平均绝对误差为:", mean_absolute_error(ss_y.inverse_transform(y_test), 57 ss_y.inverse_transform(uni_knr_y_predict))) 58 # 距离加权k近邻回归 模型评估 59 print("距离加权k近邻回归的默认评估值为:", dis_knr.score(x_test, y_test)) 60 print("距离加权k近邻回归的R_squared值为:", r2_score(y_test, dis_knr_y_predict)) 61 print("距离加权k近邻回归的均方误差为:", mean_squared_error(ss_y.inverse_transform(y_test), 62 ss_y.inverse_transform(dis_knr_y_predict))) 63 print("距离加权k近邻回归的平均绝对误差为:", mean_absolute_error(ss_y.inverse_transform(y_test), 64 ss_y.inverse_transform(dis_knr_y_predict))) 65 66 ''' 67 平均k近邻回归的默认评估值为: 0.6903454564606561 68 平均k近邻回归的R_squared值为: 0.6903454564606561 69 平均k近邻回归的均方误差为: 24.01101417322835 70 平均k近邻回归 的平均绝对误差为: 2.9680314960629928 71 距离加权k近邻回归的默认评估值为: 0.7197589970156353 72 距离加权k近邻回归的R_squared值为: 0.7197589970156353 73 距离加权k近邻回归的均方误差为: 21.730250160926044 74 距离加权k近邻回归的平均绝对误差为: 2.8050568785108005 75 '''