python实现集成回归算法,包括随机森林,极端随机森林,梯度boosting算法

from sklearn.datasets import load_boston

boston = load_boston()

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 'The max target value is: ', np.max(boston.target)
print 'The min target value is: ', np.min(boston.target)
print 'The average terget value is: ', 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)
y_test = ss_y.transform(y_test)

from sklearn.ensemble import RandomForestRegressor, ExtraTreesRegressor, GradientBoostingRegressor

rfr = RandomForestRegressor()
rfr.fit(X_test, y_test)
rfr_y_predict = rfr.predict(X_test)

etr = ExtraTreesRegressor()
etr.fit(X_train, y_train)
etr_y_predict = etr.predict(X_test)

gbr = GradientBoostingRegressor()
gbr.fit(X_train, y_train)
gbr_y_predict = gbr.predict(X_test)

from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error

print '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 'R-squared of ExtraTreesRegressor is: ', etr.score(X_test, y_test)
print 'the value of mean squared error of ExtraTreesRegressor is: ', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(etr_y_predict))
print 'the value of mean ssbsolute error of ExtraTreesRegressor is: ', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(etr_y_predict))

print 'R-squared of GradientBoostingRegressor is: ', gbr.score(X_test, y_test)
print 'the value of mean squared error of GradientBoostingRegressor is: ', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(gbr_y_predict))
print 'the value of mean ssbsolute error of GradientBoostingRegressor is: ', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(gbr_y_predict))


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