#美国波士顿地区房价描述
from sklearn.datasets import load_boston
boston=load_boston()
print(boston.DESCR)
#数据分割
from sklearn.cross_validation import train_test_split
import numpy as np
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)
#分析回归目标值的差异
print('最大值:',np.max(boston.target))
print('最小值:',np.min(boston.target))
print('平均值:',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.tree import DecisionTreeRegressor
dtr=DecisionTreeRegressor()
dtr.fit(X_train,y_train)
dtr_y_predict=dtr.predict(X_test)
#对不同配置的模型的预测进行预测性能评估
print('dtr 模型自带评分机制:',uni_knr.score(X_test,y_test))
print('r方:',r2_score(ss_y.inverse_transform(y_test),ss_y.inverse_transform(dtr_y_predict)))
print('均方误差MSE:',mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(dtr_y_predict)))
print('平均绝对误差MAE:',mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(dtr_y_predict)))