2011-11-15 04:53:47 +08:00
|
|
|
"""
|
|
|
|
|
============================
|
|
|
|
|
Gradient Boosting regression
|
|
|
|
|
============================
|
|
|
|
|
|
2015-01-16 04:09:35 +08:00
|
|
|
Demonstrate Gradient Boosting on the Boston housing dataset.
|
2011-11-15 04:53:47 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-11-15 04:53:47 +08:00
|
|
|
|
|
|
|
|
# Author: Peter Prettenhofer <peter.prettenhofer@gmail.com>
|
|
|
|
|
#
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2011-11-15 04:53:47 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2014-02-27 17:22:21 +08:00
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
2011-11-15 04:53:47 +08:00
|
|
|
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
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
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:]
|
|
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-11-15 04:53:47 +08:00
|
|
|
# Fit regression model
|
2014-07-10 14:48:20 +08:00
|
|
|
params = {'n_estimators': 500, 'max_depth': 4, 'min_samples_split': 2,
|
2012-11-03 00:07:21 +08:00
|
|
|
'learning_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)
|
|
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-11-15 05:34:50 +08:00
|
|
|
# Plot training deviance
|
|
|
|
|
|
|
|
|
|
# compute test set deviance
|
2012-03-20 22:59:29 +08:00
|
|
|
test_score = np.zeros((params['n_estimators'],), dtype=np.float64)
|
2012-03-29 16:26:43 +08:00
|
|
|
|
2015-11-05 06:11:25 +08:00
|
|
|
for i, y_pred in enumerate(clf.staged_predict(X_test)):
|
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
|
|
|
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.figure(figsize=(12, 6))
|
|
|
|
|
plt.subplot(1, 2, 1)
|
|
|
|
|
plt.title('Deviance')
|
|
|
|
|
plt.plot(np.arange(params['n_estimators']) + 1, clf.train_score_, 'b-',
|
2014-05-15 12:05:24 +08:00
|
|
|
label='Training Set Deviance')
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.plot(np.arange(params['n_estimators']) + 1, test_score, 'r-',
|
2014-05-15 12:05:24 +08:00
|
|
|
label='Test Set Deviance')
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.legend(loc='upper right')
|
|
|
|
|
plt.xlabel('Boosting Iterations')
|
|
|
|
|
plt.ylabel('Deviance')
|
2011-11-15 04:53:47 +08:00
|
|
|
|
2017-06-20 20:48:57 +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
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.subplot(1, 2, 2)
|
|
|
|
|
plt.barh(pos, feature_importance[sorted_idx], align='center')
|
|
|
|
|
plt.yticks(pos, boston.feature_names[sorted_idx])
|
|
|
|
|
plt.xlabel('Relative Importance')
|
|
|
|
|
plt.title('Variable Importance')
|
|
|
|
|
plt.show()
|