Skip to content

Commit 7edd108

Browse files
committed
Pushing the docs to dev/ for branch: master, commit bab5926024e6ce43b6211ab71ffa3d2872be60ca
1 parent f949557 commit 7edd108

File tree

1,212 files changed

+6517
-4037
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,212 files changed

+6517
-4037
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""
2+
=================================
3+
Combine predictors using stacking
4+
=================================
5+
6+
Stacking refers to a method to blend estimators. In this strategy, some
7+
estimators are individually fitted on some training data while a final
8+
estimator is trained using the stacked predictions of these base estimators.
9+
10+
In this example, we illustrate the use case in which different regressors are
11+
stacked together and a final linear penalized regressor is used to output the
12+
prediction. We compare the performance of each individual regressor with the
13+
stacking strategy. Stacking slightly improves the overall performance.
14+
15+
"""
16+
print(__doc__)
17+
18+
# Authors: Guillaume Lemaitre <[email protected]>
19+
# License: BSD 3 clause
20+
21+
###############################################################################
22+
# The function ``plot_regression_results`` is used to plot the predicted and
23+
# true targets.
24+
25+
import matplotlib.pyplot as plt
26+
27+
28+
def plot_regression_results(ax, y_true, y_pred, title, scores, elapsed_time):
29+
"""Scatter plot of the predicted vs true targets."""
30+
ax.plot([y_true.min(), y_true.max()],
31+
[y_true.min(), y_true.max()],
32+
'--r', linewidth=2)
33+
ax.scatter(y_true, y_pred, alpha=0.2)
34+
35+
ax.spines['top'].set_visible(False)
36+
ax.spines['right'].set_visible(False)
37+
ax.get_xaxis().tick_bottom()
38+
ax.get_yaxis().tick_left()
39+
ax.spines['left'].set_position(('outward', 10))
40+
ax.spines['bottom'].set_position(('outward', 10))
41+
ax.set_xlim([y_true.min(), y_true.max()])
42+
ax.set_ylim([y_true.min(), y_true.max()])
43+
ax.set_xlabel('Measured')
44+
ax.set_ylabel('Predicted')
45+
extra = plt.Rectangle((0, 0), 0, 0, fc="w", fill=False,
46+
edgecolor='none', linewidth=0)
47+
ax.legend([extra], [scores], loc='upper left')
48+
title = title + '\n Evaluation in {:.2f} seconds'.format(elapsed_time)
49+
ax.set_title(title)
50+
51+
52+
###############################################################################
53+
# Stack of predictors on a single data set
54+
###############################################################################
55+
# It is sometimes tedious to find the model which will best perform on a given
56+
# dataset. Stacking provide an alternative by combining the outputs of several
57+
# learners, without the need to choose a model specifically. The performance of
58+
# stacking is usually close to the best model and sometimes it can outperform
59+
# the prediction performance of each individual model.
60+
#
61+
# Here, we combine 3 learners (linear and non-linear) and use a ridge regressor
62+
# to combine their outputs together.
63+
64+
from sklearn.ensemble import StackingRegressor
65+
from sklearn.ensemble import RandomForestRegressor
66+
from sklearn.experimental import enable_hist_gradient_boosting # noqa
67+
from sklearn.ensemble import HistGradientBoostingRegressor
68+
from sklearn.linear_model import LassoCV
69+
from sklearn.linear_model import RidgeCV
70+
71+
estimators = [
72+
('Random Forest', RandomForestRegressor(random_state=42)),
73+
('Lasso', LassoCV()),
74+
('Gradient Boosting', HistGradientBoostingRegressor(random_state=0))
75+
]
76+
stacking_regressor = StackingRegressor(
77+
estimators=estimators, final_estimator=RidgeCV()
78+
)
79+
80+
81+
###############################################################################
82+
# We used the Boston data set (prediction of house prices). We check the
83+
# performance of each individual predictor as well as the stack of the
84+
# regressors.
85+
86+
import time
87+
import numpy as np
88+
from sklearn.datasets import load_boston
89+
from sklearn.model_selection import cross_validate, cross_val_predict
90+
91+
X, y = load_boston(return_X_y=True)
92+
93+
fig, axs = plt.subplots(2, 2, figsize=(9, 7))
94+
axs = np.ravel(axs)
95+
96+
for ax, (name, est) in zip(axs, estimators + [('Stacking Regressor',
97+
stacking_regressor)]):
98+
start_time = time.time()
99+
score = cross_validate(est, X, y,
100+
scoring=['r2', 'neg_mean_absolute_error'],
101+
n_jobs=-1, verbose=0)
102+
elapsed_time = time.time() - time.time()
103+
104+
y_pred = cross_val_predict(est, X, y, n_jobs=-1, verbose=0)
105+
plot_regression_results(
106+
ax, y, y_pred,
107+
name,
108+
(r'$R^2={:.2f} \pm {:.2f}$' + '\n' + r'$MAE={:.2f} \pm {:.2f}$')
109+
.format(np.mean(score['test_r2']),
110+
np.std(score['test_r2']),
111+
-np.mean(score['test_neg_mean_absolute_error']),
112+
np.std(score['test_neg_mean_absolute_error'])),
113+
elapsed_time)
114+
115+
plt.suptitle('Single predictors versus stacked predictors')
116+
plt.tight_layout()
117+
plt.subplots_adjust(top=0.9)
118+
plt.show()
119+
120+
###############################################################################
121+
# The stacked regressor will combine the strengths of the different regressors.
122+
# However, we also see that training the stacked regressor is much more
123+
# computationally expensive.
Binary file not shown.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"%matplotlib inline"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"\n# Combine predictors using stacking\n\n\nStacking refers to a method to blend estimators. In this strategy, some\nestimators are individually fitted on some training data while a final\nestimator is trained using the stacked predictions of these base estimators.\n\nIn this example, we illustrate the use case in which different regressors are\nstacked together and a final linear penalized regressor is used to output the\nprediction. We compare the performance of each individual regressor with the\nstacking strategy. Stacking slightly improves the overall performance.\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"print(__doc__)\n\n# Authors: Guillaume Lemaitre <[email protected]>\n# License: BSD 3 clause"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"The function ``plot_regression_results`` is used to plot the predicted and\ntrue targets.\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"import matplotlib.pyplot as plt\n\n\ndef plot_regression_results(ax, y_true, y_pred, title, scores, elapsed_time):\n \"\"\"Scatter plot of the predicted vs true targets.\"\"\"\n ax.plot([y_true.min(), y_true.max()],\n [y_true.min(), y_true.max()],\n '--r', linewidth=2)\n ax.scatter(y_true, y_pred, alpha=0.2)\n\n ax.spines['top'].set_visible(False)\n ax.spines['right'].set_visible(False)\n ax.get_xaxis().tick_bottom()\n ax.get_yaxis().tick_left()\n ax.spines['left'].set_position(('outward', 10))\n ax.spines['bottom'].set_position(('outward', 10))\n ax.set_xlim([y_true.min(), y_true.max()])\n ax.set_ylim([y_true.min(), y_true.max()])\n ax.set_xlabel('Measured')\n ax.set_ylabel('Predicted')\n extra = plt.Rectangle((0, 0), 0, 0, fc=\"w\", fill=False,\n edgecolor='none', linewidth=0)\n ax.legend([extra], [scores], loc='upper left')\n title = title + '\\n Evaluation in {:.2f} seconds'.format(elapsed_time)\n ax.set_title(title)"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"Stack of predictors on a single data set\n##############################################################################\n It is sometimes tedious to find the model which will best perform on a given\n dataset. Stacking provide an alternative by combining the outputs of several\n learners, without the need to choose a model specifically. The performance of\n stacking is usually close to the best model and sometimes it can outperform\n the prediction performance of each individual model.\n\n Here, we combine 3 learners (linear and non-linear) and use a ridge regressor\n to combine their outputs together.\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"from sklearn.ensemble import StackingRegressor\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.experimental import enable_hist_gradient_boosting # noqa\nfrom sklearn.ensemble import HistGradientBoostingRegressor\nfrom sklearn.linear_model import LassoCV\nfrom sklearn.linear_model import RidgeCV\n\nestimators = [\n ('Random Forest', RandomForestRegressor(random_state=42)),\n ('Lasso', LassoCV()),\n ('Gradient Boosting', HistGradientBoostingRegressor(random_state=0))\n]\nstacking_regressor = StackingRegressor(\n estimators=estimators, final_estimator=RidgeCV()\n)"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"We used the Boston data set (prediction of house prices). We check the\nperformance of each individual predictor as well as the stack of the\nregressors.\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"import time\nimport numpy as np\nfrom sklearn.datasets import load_boston\nfrom sklearn.model_selection import cross_validate, cross_val_predict\n\nX, y = load_boston(return_X_y=True)\n\nfig, axs = plt.subplots(2, 2, figsize=(9, 7))\naxs = np.ravel(axs)\n\nfor ax, (name, est) in zip(axs, estimators + [('Stacking Regressor',\n stacking_regressor)]):\n start_time = time.time()\n score = cross_validate(est, X, y,\n scoring=['r2', 'neg_mean_absolute_error'],\n n_jobs=-1, verbose=0)\n elapsed_time = time.time() - time.time()\n\n y_pred = cross_val_predict(est, X, y, n_jobs=-1, verbose=0)\n plot_regression_results(\n ax, y, y_pred,\n name,\n (r'$R^2={:.2f} \\pm {:.2f}$' + '\\n' + r'$MAE={:.2f} \\pm {:.2f}$')\n .format(np.mean(score['test_r2']),\n np.std(score['test_r2']),\n -np.mean(score['test_neg_mean_absolute_error']),\n np.std(score['test_neg_mean_absolute_error'])),\n elapsed_time)\n\nplt.suptitle('Single predictors versus stacked predictors')\nplt.tight_layout()\nplt.subplots_adjust(top=0.9)\nplt.show()"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"The stacked regressor will combine the strengths of the different regressors.\nHowever, we also see that training the stacked regressor is much more\ncomputationally expensive.\n\n"
91+
]
92+
}
93+
],
94+
"metadata": {
95+
"kernelspec": {
96+
"display_name": "Python 3",
97+
"language": "python",
98+
"name": "python3"
99+
},
100+
"language_info": {
101+
"codemirror_mode": {
102+
"name": "ipython",
103+
"version": 3
104+
},
105+
"file_extension": ".py",
106+
"mimetype": "text/x-python",
107+
"name": "python",
108+
"nbconvert_exporter": "python",
109+
"pygments_lexer": "ipython3",
110+
"version": "3.7.4"
111+
}
112+
},
113+
"nbformat": 4,
114+
"nbformat_minor": 0
115+
}
Binary file not shown.

dev/_downloads/scikit-learn-docs.pdf

180 KB
Binary file not shown.

dev/_images/iris.png

0 Bytes
42 Bytes
42 Bytes
-595 Bytes
-595 Bytes

0 commit comments

Comments
 (0)