2012-03-06 05:26:06 +08:00
|
|
|
"""
|
|
|
|
|
================================
|
|
|
|
|
Gradient Boosting regularization
|
|
|
|
|
================================
|
|
|
|
|
|
|
|
|
|
Illustration of the effect of different regularization strategies
|
2017-07-30 13:22:10 +08:00
|
|
|
for Gradient Boosting. The example is taken from Hastie et al 2009 [1]_.
|
2012-03-06 05:26:06 +08:00
|
|
|
|
2012-07-08 01:08:29 +08:00
|
|
|
The loss function used is binomial deviance. Regularization via
|
2012-11-03 00:07:21 +08:00
|
|
|
shrinkage (``learning_rate < 1.0``) improves performance considerably.
|
2012-07-08 01:08:29 +08:00
|
|
|
In combination with shrinkage, stochastic gradient boosting
|
|
|
|
|
(``subsample < 1.0``) can produce more accurate models by reducing the
|
|
|
|
|
variance via bagging.
|
2012-03-13 05:20:04 +08:00
|
|
|
Subsampling without shrinkage usually does poorly.
|
2012-07-08 01:08:29 +08:00
|
|
|
Another strategy to reduce the variance is by subsampling the features
|
|
|
|
|
analogous to the random splits in Random Forests
|
|
|
|
|
(via the ``max_features`` parameter).
|
2012-03-13 05:20:04 +08:00
|
|
|
|
2012-03-06 05:26:06 +08:00
|
|
|
.. [1] T. Hastie, R. Tibshirani and J. Friedman, "Elements of Statistical
|
|
|
|
|
Learning Ed. 2", Springer, 2009.
|
2021-10-22 21:33:22 +08:00
|
|
|
|
2012-03-06 05:26:06 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Author: Peter Prettenhofer <peter.prettenhofer@gmail.com>
|
|
|
|
|
#
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-03-06 05:26:06 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2014-02-27 17:22:21 +08:00
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
2012-03-06 05:26:06 +08:00
|
|
|
from sklearn import ensemble
|
|
|
|
|
from sklearn import datasets
|
|
|
|
|
|
2012-03-13 05:20:04 +08:00
|
|
|
|
2012-03-06 05:26:06 +08:00
|
|
|
X, y = datasets.make_hastie_10_2(n_samples=12000, random_state=1)
|
|
|
|
|
X = X.astype(np.float32)
|
|
|
|
|
|
2013-01-26 19:19:40 +08:00
|
|
|
# map labels from {-1, 1} to {0, 1}
|
2014-01-06 21:45:40 +08:00
|
|
|
labels, y = np.unique(y, return_inverse=True)
|
2013-01-26 19:19:40 +08:00
|
|
|
|
2012-03-06 05:26:06 +08:00
|
|
|
X_train, X_test = X[:2000], X[2000:]
|
|
|
|
|
y_train, y_test = y[:2000], y[2000:]
|
|
|
|
|
|
2021-06-18 21:46:40 +08:00
|
|
|
original_params = {
|
|
|
|
|
"n_estimators": 1000,
|
|
|
|
|
"max_leaf_nodes": 4,
|
|
|
|
|
"max_depth": None,
|
|
|
|
|
"random_state": 2,
|
|
|
|
|
"min_samples_split": 5,
|
|
|
|
|
}
|
2012-03-06 05:26:06 +08:00
|
|
|
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.figure()
|
2012-03-06 05:26:06 +08:00
|
|
|
|
|
|
|
|
for label, color, setting in [
|
2012-11-03 00:07:21 +08:00
|
|
|
("No shrinkage", "orange", {"learning_rate": 1.0, "subsample": 1.0}),
|
|
|
|
|
("learning_rate=0.1", "turquoise", {"learning_rate": 0.1, "subsample": 1.0}),
|
|
|
|
|
("subsample=0.5", "blue", {"learning_rate": 1.0, "subsample": 0.5}),
|
|
|
|
|
(
|
|
|
|
|
"learning_rate=0.1, subsample=0.5",
|
|
|
|
|
"gray",
|
|
|
|
|
{"learning_rate": 0.1, "subsample": 0.5},
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"learning_rate=0.1, max_features=2",
|
|
|
|
|
"magenta",
|
|
|
|
|
{"learning_rate": 0.1, "max_features": 2},
|
2021-10-07 16:13:00 +08:00
|
|
|
),
|
2012-11-03 00:07:21 +08:00
|
|
|
]:
|
2012-03-06 05:26:06 +08:00
|
|
|
params = dict(original_params)
|
|
|
|
|
params.update(setting)
|
|
|
|
|
|
|
|
|
|
clf = ensemble.GradientBoostingClassifier(**params)
|
|
|
|
|
clf.fit(X_train, y_train)
|
|
|
|
|
|
|
|
|
|
# compute test set deviance
|
|
|
|
|
test_deviance = np.zeros((params["n_estimators"],), dtype=np.float64)
|
2012-03-29 16:26:43 +08:00
|
|
|
|
|
|
|
|
for i, y_pred in enumerate(clf.staged_decision_function(X_test)):
|
2013-01-26 19:19:40 +08:00
|
|
|
# clf.loss_ assumes that y_test[i] in {0, 1}
|
2012-03-13 05:20:04 +08:00
|
|
|
test_deviance[i] = clf.loss_(y_test, y_pred)
|
2012-03-06 05:26:06 +08:00
|
|
|
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.plot(
|
|
|
|
|
(np.arange(test_deviance.shape[0]) + 1)[::5],
|
|
|
|
|
test_deviance[::5],
|
2021-06-18 21:46:40 +08:00
|
|
|
"-",
|
|
|
|
|
color=color,
|
|
|
|
|
label=label,
|
|
|
|
|
)
|
2021-10-07 16:13:00 +08:00
|
|
|
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.legend(loc="upper left")
|
|
|
|
|
plt.xlabel("Boosting Iterations")
|
|
|
|
|
plt.ylabel("Test Set Deviance")
|
2012-03-06 05:26:06 +08:00
|
|
|
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.show()
|