集成模型(回归)对Boston数据进行训练并对数据做出预测

from sklearn.datasets import load_boston
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestRegressor,ExtraTreesRegressor,GradientBoostingRegressor
from sklearn.metrics import mean_squared_error,mean_absolute_error
#导入数据
boston = load_boston()
#查看数据信息print(boston.DESCR)
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)
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)
#使用RandomForestRegressor训练模型,并针对数据做出预测
rfr = RandomForestRegressor()
rfr.fit(X_train,y_train)
rfr_y_predict = rfr.predict(X_test)


#使用ExtraTreesRegressor训练模型,并对测试数据进行预测
etr = ExtraTreesRegressor()
etr.fit(X_train,y_train)
etr_y_predict = etr.predict(X_test)


#使用GradientBoostingRegressor训练模型,并对测试数据进行预测
gbr = GradientBoostingRegressor()
gbr.fit(X_train,y_train)
gbr_y_predict = gbr.predict(X_test)




#使用R-squared,MSE以及MAE三种指标对默认配置的回归树在测试集上进行性能评估
print('R-squared value of RandomForestRegressor:',rfr.score(X_test,y_test))
print('\n'*2)
print('The mean squared error of RandomForestRegressor: ',mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(rfr_y_predict)))
print('\n'*2)
print('The mean absolute error of RandomForestRegressor:',mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(rfr_y_predict)))
print('\n'*2)
print('R-squared value of ExtraTreesRegressor:',etr.score(X_test,y_test))
print('\n'*2)
print('The mean squared error of ExtraTreesRegressor: ',mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(etr_y_predict)))
print('\n'*2)
print('The mean absolute error of ExtraTreesRegressor:',mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(etr_y_predict)))
print('\n'*2)
print('R-squared value of GradientBoostingRegressor:',gbr.score(X_test,y_test))
print('\n'*2)
print('The mean squared error of GradientBoostingRegressor: ',mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(gbr_y_predict)))
print('\n'*2)
print('The mean absolute error of GradientBoostingRegressor:',mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(gbr_y_predict)))
print('\n'*2)

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