2011-12-30 11:17:30 +08:00
|
|
|
"""
|
2013-02-03 10:37:38 +08:00
|
|
|
======================================
|
2013-01-24 21:08:51 +08:00
|
|
|
Decision Tree Regression with AdaBoost
|
2013-02-03 10:37:38 +08:00
|
|
|
======================================
|
2011-12-30 11:17:30 +08:00
|
|
|
|
2013-02-03 10:37:38 +08:00
|
|
|
A decision tree is boosted using the AdaBoost.R2 [1] algorithm on a 1D
|
|
|
|
|
sinusoidal dataset with a small amount of Gaussian noise.
|
|
|
|
|
299 boosts (300 decision trees) is compared with a single decision tree
|
|
|
|
|
regressor. As the number of boosts is increased the regressor can fit more
|
|
|
|
|
detail.
|
|
|
|
|
|
|
|
|
|
.. [1] H. Drucker, "Improving Regressors using Boosting Techniques", 1997.
|
2011-12-30 11:17:30 +08:00
|
|
|
|
|
|
|
|
"""
|
2013-02-12 06:11:57 +08:00
|
|
|
print(__doc__)
|
2011-12-30 11:17:30 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
2013-02-03 10:37:38 +08:00
|
|
|
# Create a the dataset
|
2011-12-30 11:17:30 +08:00
|
|
|
rng = np.random.RandomState(1)
|
2013-02-03 10:37:38 +08:00
|
|
|
X = np.linspace(0, 6, 100)[:, np.newaxis]
|
|
|
|
|
y = np.sin(X).ravel() + np.sin(6 * X).ravel() + rng.normal(0, 0.1, X.shape[0])
|
2011-12-30 11:17:30 +08:00
|
|
|
|
|
|
|
|
# Fit regression model
|
|
|
|
|
from sklearn.tree import DecisionTreeRegressor
|
|
|
|
|
from sklearn.ensemble import AdaBoostRegressor
|
|
|
|
|
|
2013-02-03 10:37:38 +08:00
|
|
|
clf_1 = DecisionTreeRegressor(max_depth=4)
|
2013-01-23 11:28:47 +08:00
|
|
|
|
2013-02-04 07:11:17 +08:00
|
|
|
clf_2 = AdaBoostRegressor(DecisionTreeRegressor(max_depth=4),
|
|
|
|
|
n_estimators=300, random_state=rng)
|
2011-12-30 11:17:30 +08:00
|
|
|
|
|
|
|
|
clf_1.fit(X, y)
|
|
|
|
|
clf_2.fit(X, y)
|
|
|
|
|
|
|
|
|
|
# Predict
|
2013-02-03 10:37:38 +08:00
|
|
|
y_1 = clf_1.predict(X)
|
|
|
|
|
y_2 = clf_2.predict(X)
|
2011-12-30 11:17:30 +08:00
|
|
|
|
|
|
|
|
# Plot the results
|
|
|
|
|
import pylab as pl
|
|
|
|
|
|
|
|
|
|
pl.figure()
|
2013-02-10 10:25:02 +08:00
|
|
|
pl.scatter(X, y, c="k", label="training samples")
|
2013-02-03 10:37:38 +08:00
|
|
|
pl.plot(X, y_1, c="g", label="n_estimators=1", linewidth=2)
|
|
|
|
|
pl.plot(X, y_2, c="r", label="n_estimators=300", linewidth=2)
|
2011-12-30 11:17:30 +08:00
|
|
|
pl.xlabel("data")
|
|
|
|
|
pl.ylabel("target")
|
|
|
|
|
pl.title("Boosted Decision Tree Regression")
|
|
|
|
|
pl.legend()
|
|
|
|
|
pl.show()
|