1 from sklearn.datasets importload_boston2 from sklearn.cross_validation importtrain_test_split3 from sklearn.preprocessing importStandardScaler4 from sklearn.neighbors importKNeighborsRegressor5 from sklearn.metrics importr2_score, mean_squared_error, mean_absolute_error6 importnumpy as np7
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.data19 y =boston.target20
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