2012-07-09 22:57:06 +08:00
|
|
|
"""
|
|
|
|
|
=====================================================
|
|
|
|
|
Prediction Intervals for Gradient Boosting Regression
|
|
|
|
|
=====================================================
|
|
|
|
|
|
|
|
|
|
This example shows how quantile regression can be used
|
|
|
|
|
to create prediction intervals.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
2014-02-27 17:22:21 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2012-07-09 22:57:06 +08:00
|
|
|
|
2014-02-27 17:22:21 +08:00
|
|
|
from sklearn.ensemble import GradientBoostingRegressor
|
2012-07-09 22:57:06 +08:00
|
|
|
|
|
|
|
|
np.random.seed(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def f(x):
|
|
|
|
|
"""The function to predict."""
|
|
|
|
|
return x * np.sin(x)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
# First the noiseless case
|
|
|
|
|
X = np.atleast_2d(np.random.uniform(0, 10.0, size=100)).T
|
|
|
|
|
X = X.astype(np.float32)
|
|
|
|
|
|
|
|
|
|
# Observations
|
|
|
|
|
y = f(X).ravel()
|
|
|
|
|
|
|
|
|
|
dy = 1.5 + 1.0 * np.random.random(y.shape)
|
|
|
|
|
noise = np.random.normal(0, dy)
|
|
|
|
|
y += noise
|
|
|
|
|
y = y.astype(np.float32)
|
|
|
|
|
|
|
|
|
|
# Mesh the input space for evaluations of the real function, the prediction and
|
|
|
|
|
# its MSE
|
|
|
|
|
xx = np.atleast_2d(np.linspace(0, 10, 1000)).T
|
|
|
|
|
xx = xx.astype(np.float32)
|
|
|
|
|
|
|
|
|
|
alpha = 0.95
|
|
|
|
|
|
|
|
|
|
clf = GradientBoostingRegressor(loss='quantile', alpha=alpha,
|
|
|
|
|
n_estimators=250, max_depth=3,
|
2018-09-08 22:43:21 +08:00
|
|
|
learning_rate=.1, min_samples_leaf=9,
|
|
|
|
|
min_samples_split=9)
|
2012-07-09 22:57:06 +08:00
|
|
|
|
|
|
|
|
clf.fit(X, y)
|
|
|
|
|
|
|
|
|
|
# Make the prediction on the meshed x-axis
|
|
|
|
|
y_upper = clf.predict(xx)
|
|
|
|
|
|
|
|
|
|
clf.set_params(alpha=1.0 - alpha)
|
|
|
|
|
clf.fit(X, y)
|
|
|
|
|
|
|
|
|
|
# Make the prediction on the meshed x-axis
|
|
|
|
|
y_lower = clf.predict(xx)
|
|
|
|
|
|
|
|
|
|
clf.set_params(loss='ls')
|
|
|
|
|
clf.fit(X, y)
|
|
|
|
|
|
|
|
|
|
# Make the prediction on the meshed x-axis
|
|
|
|
|
y_pred = clf.predict(xx)
|
|
|
|
|
|
2014-05-18 04:28:30 +08:00
|
|
|
# Plot the function, the prediction and the 90% confidence interval based on
|
2012-07-09 22:57:06 +08:00
|
|
|
# the MSE
|
2014-02-27 17:22:21 +08:00
|
|
|
fig = plt.figure()
|
2019-01-05 12:22:36 +08:00
|
|
|
plt.plot(xx, f(xx), 'g:', label=r'$f(x) = x\,\sin(x)$')
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.plot(X, y, 'b.', markersize=10, label=u'Observations')
|
|
|
|
|
plt.plot(xx, y_pred, 'r-', label=u'Prediction')
|
|
|
|
|
plt.plot(xx, y_upper, 'k-')
|
|
|
|
|
plt.plot(xx, y_lower, 'k-')
|
|
|
|
|
plt.fill(np.concatenate([xx, xx[::-1]]),
|
2014-05-15 12:05:24 +08:00
|
|
|
np.concatenate([y_upper, y_lower[::-1]]),
|
2014-05-18 04:28:30 +08:00
|
|
|
alpha=.5, fc='b', ec='None', label='90% prediction interval')
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.xlabel('$x$')
|
|
|
|
|
plt.ylabel('$f(x)$')
|
|
|
|
|
plt.ylim(-10, 20)
|
|
|
|
|
plt.legend(loc='upper left')
|
|
|
|
|
plt.show()
|