本文不具体介绍回归树的具体算法,采用波士顿房价预测的案例来使用回归树模型。语言是Python3.6,集成环境是Anaconda3。
#导入数据
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,random_state=33,test_size=0.25)
#分析回归目标值的差异
print('max:',np.max(boston.target),'\tmin:',np.min(boston.target),'\taverage:',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.fit_transform(X_test)
y_train=ss_y.fit_transform(y_train)
y_test=ss_y.fit_transform(y_test)
#导入回归树
from sklearn.tree import DecisionTreeRegressor
#使用默认配置初始化DecisionTreeRegressor
dtr=DecisionTreeRegressor()
dtr.fit(X_train,y_train)
dtr_y_predict=dtr.predict(X_test)
#性能测评
print('R-squared value of DecisionTreeRegressor:',dtr.score(X_test,y_test))