使用三种继承回归模型对美国波士顿房价训练数据进行学习,并对测试数据进行预测

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=33)#25%数据用于测试,75%数据用于训练


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.ensemble import RandomForestRegressor, ExtraTreesRegressor, GradientBoostingRegressor
rfr = RandomForestRegressor()
rfr.fit(x_train, y_train.ravel())
rfr_y_predict = rfr.predict(x_test)

etr = ExtraTreesRegressor()
etr.fit(x_train, y_train.ravel())
etr_y_predict = etr.predict(x_test)

gbr = GradientBoostingRegressor()
gbr.fit(x_train, y_train.ravel())
gbr_y_predict = gbr.predict(x_test)
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error

print('The R-squared value of RandomForestRegressor is', rfr.score(x_test, y_test))
print('The mean_squared_error of RandomForestRegressor is', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(rfr_y_predict)))
print('The mean_absolute_error of RandomForestRegressor is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(rfr_y_predict)))

print('\nThe R-squared value of ExtraTreesRegressor is', etr.score(x_test, y_test))
print('The mean_squared_error of ExtraTreesRegressor is', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(etr_y_predict)))
print('The mean_absolute_error of ExtraTreesRegressor is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(etr_y_predict)))

print('\nThe R-squared value of GradientBoostingRegressor is', gbr.score(x_test, y_test))
print('The mean_squared_error of GradientBoostingRegressor  is', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(gbr_y_predict)))
print('The mean_absolute_error of GradientBoostingRegressor  is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(gbr_y_predict)))

运行结果如下:

The R-squared value of RandomForestRegressor is 0.813955467991
The mean_squared_error of RandomForestRegressor is 14.4261338583
The mean_absolute_error of RandomForestRegressor is 2.49023622047

The R-squared value of ExtraTreesRegressor is 0.777675210793
The mean_squared_error of ExtraTreesRegressor is 17.2393519685
The mean_absolute_error of ExtraTreesRegressor is 2.63692913386

The R-squared value of GradientBoostingRegressor is 0.837081905694
The mean_squared_error of GradientBoostingRegressor  is 12.6328799402
The mean_absolute_error of GradientBoostingRegressor  is 2.27414776691


你可能感兴趣的:(python机器学习及实战)