2011-11-15 04:53:47 +08:00
|
|
|
"""
|
|
|
|
|
============================
|
|
|
|
|
Gradient Boosting regression
|
|
|
|
|
============================
|
|
|
|
|
|
|
|
|
|
Demonstrate Gradient Boosting on the boston housing dataset.
|
|
|
|
|
|
|
|
|
|
This example fits a Gradient Boosting model with least squares loss and
|
2012-03-13 05:20:04 +08:00
|
|
|
500 regression trees of depth 4.
|
2011-11-15 04:53:47 +08:00
|
|
|
"""
|
|
|
|
|
print __doc__
|
|
|
|
|
|
|
|
|
|
# Author: Peter Prettenhofer <peter.prettenhofer@gmail.com>
|
|
|
|
|
#
|
|
|
|
|
# License: BSD
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import pylab as pl
|
|
|
|
|
from sklearn import ensemble
|
|
|
|
|
from sklearn import datasets
|
|
|
|
|
from sklearn.utils import shuffle
|
2012-02-02 02:49:31 +08:00
|
|
|
from sklearn.metrics import mean_squared_error
|
2011-11-15 04:53:47 +08:00
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
# Load data
|
|
|
|
|
boston = datasets.load_boston()
|
|
|
|
|
X, y = shuffle(boston.data, boston.target, random_state=13)
|
2011-11-15 05:34:50 +08:00
|
|
|
X = X.astype(np.float32)
|
2011-11-15 04:53:47 +08:00
|
|
|
offset = int(X.shape[0] * 0.9)
|
|
|
|
|
X_train, y_train = X[:offset], y[:offset]
|
|
|
|
|
X_test, y_test = X[offset:], y[offset:]
|
|
|
|
|
|
2012-03-20 22:59:29 +08:00
|
|
|
###############################################################################
|
2011-11-15 04:53:47 +08:00
|
|
|
# Fit regression model
|
2012-03-06 05:26:06 +08:00
|
|
|
params = {'n_estimators': 500, 'max_depth': 4, 'min_samples_split': 1,
|
|
|
|
|
'learn_rate': 0.01, 'loss': 'ls'}
|
2011-11-15 04:53:47 +08:00
|
|
|
clf = ensemble.GradientBoostingRegressor(**params)
|
|
|
|
|
|
|
|
|
|
clf.fit(X_train, y_train)
|
2012-02-02 02:49:31 +08:00
|
|
|
mse = mean_squared_error(y_test, clf.predict(X_test))
|
2011-11-15 05:34:50 +08:00
|
|
|
print("MSE: %.4f" % mse)
|
|
|
|
|
|
2012-03-20 22:59:29 +08:00
|
|
|
###############################################################################
|
2011-11-15 05:34:50 +08:00
|
|
|
# Plot training deviance
|
|
|
|
|
|
|
|
|
|
# compute test set deviance
|
|
|
|
|
y_pred = clf.init.predict(X_test)
|
2012-03-20 22:59:29 +08:00
|
|
|
test_score = np.zeros((params['n_estimators'],), dtype=np.float64)
|
2012-03-13 05:20:04 +08:00
|
|
|
for i, stage in enumerate(clf.estimators_):
|
|
|
|
|
y_pred = clf._predict(X_test, old_pred=y_pred, stage_index=i)
|
2012-03-20 22:59:29 +08:00
|
|
|
test_score[i] = clf.loss_(y_test, y_pred)
|
2011-11-15 04:53:47 +08:00
|
|
|
|
2012-03-13 05:20:04 +08:00
|
|
|
pl.figure(figsize=(12, 6))
|
2011-11-24 06:07:32 +08:00
|
|
|
pl.subplot(1, 2, 1)
|
2011-11-15 04:53:47 +08:00
|
|
|
pl.title('Deviance')
|
2012-03-20 22:59:29 +08:00
|
|
|
pl.plot(np.arange(params['n_estimators']) + 1, clf.train_score_, 'b-',
|
2011-11-15 05:34:50 +08:00
|
|
|
label='Training Set Deviance')
|
2012-03-20 22:59:29 +08:00
|
|
|
pl.plot(np.arange(params['n_estimators']) + 1, test_score, 'r-',
|
2011-11-15 05:34:50 +08:00
|
|
|
label='Test Set Deviance')
|
|
|
|
|
pl.legend(loc='upper right')
|
2011-11-15 04:53:47 +08:00
|
|
|
pl.xlabel('Boosting Iterations')
|
2011-11-15 05:34:50 +08:00
|
|
|
pl.ylabel('Deviance')
|
2011-11-15 04:53:47 +08:00
|
|
|
|
2012-03-20 22:59:29 +08:00
|
|
|
###############################################################################
|
2012-01-07 04:38:10 +08:00
|
|
|
# Plot feature importance
|
|
|
|
|
feature_importance = clf.feature_importances_
|
2012-03-20 22:59:29 +08:00
|
|
|
# make importances relative to max importance
|
|
|
|
|
feature_importance = 100.0 * (feature_importance / feature_importance.max())
|
2012-01-07 04:38:10 +08:00
|
|
|
sorted_idx = np.argsort(feature_importance)
|
2011-11-15 04:53:47 +08:00
|
|
|
pos = np.arange(sorted_idx.shape[0]) + .5
|
2011-11-24 06:07:32 +08:00
|
|
|
pl.subplot(1, 2, 2)
|
2012-01-07 04:38:10 +08:00
|
|
|
pl.barh(pos, feature_importance[sorted_idx], align='center')
|
2011-11-15 04:53:47 +08:00
|
|
|
pl.yticks(pos, boston.feature_names[sorted_idx])
|
2011-11-15 05:34:50 +08:00
|
|
|
pl.xlabel('Relative Importance')
|
|
|
|
|
pl.title('Variable Importance')
|
2011-11-15 04:53:47 +08:00
|
|
|
pl.show()
|