Skip to content

Commit 1ad7bd0

Browse files
committed
Pushing the docs to dev/ for branch: master, commit 36ebf3e371a10ba2377199f04bf5c626b0fbc43c
1 parent ac52cdb commit 1ad7bd0

File tree

1,220 files changed

+4872
-4024
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,220 files changed

+4872
-4024
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
=====================
3+
Monotonic Constraints
4+
=====================
5+
6+
This example illustrates the effect of monotonic constraints on a gradient
7+
boosting estimator.
8+
9+
We build an artificial dataset where the target value is in general
10+
positively correlated with the first feature (with some random and
11+
non-random variations), and in general negatively correlated with the second
12+
feature.
13+
14+
By imposing a positive (increasing) or negative (decreasing) constraint on
15+
the features during the learning process, the estimator is able to properly
16+
follow the general trend instead of being subject to the variations.
17+
18+
This example was inspired by the `XGBoost documentation
19+
<https://xgboost.readthedocs.io/en/latest/tutorials/monotonic.html>`_.
20+
"""
21+
from sklearn.experimental import enable_hist_gradient_boosting # noqa
22+
from sklearn.ensemble import HistGradientBoostingRegressor
23+
from sklearn.inspection import plot_partial_dependence
24+
import numpy as np
25+
import matplotlib.pyplot as plt
26+
27+
28+
print(__doc__)
29+
30+
rng = np.random.RandomState(0)
31+
32+
n_samples = 5000
33+
f_0 = rng.rand(n_samples) # positive correlation with y
34+
f_1 = rng.rand(n_samples) # negative correlation with y
35+
X = np.c_[f_0, f_1]
36+
noise = rng.normal(loc=0.0, scale=0.01, size=n_samples)
37+
y = (5 * f_0 + np.sin(10 * np.pi * f_0) -
38+
5 * f_1 - np.cos(10 * np.pi * f_1) +
39+
noise)
40+
41+
fig, ax = plt.subplots()
42+
43+
44+
# Without any constraint
45+
gbdt = HistGradientBoostingRegressor()
46+
gbdt.fit(X, y)
47+
disp = plot_partial_dependence(
48+
gbdt, X, features=[0, 1],
49+
line_kw={'linewidth': 4, 'label': 'unconstrained'},
50+
ax=ax)
51+
52+
# With positive and negative constraints
53+
gbdt = HistGradientBoostingRegressor(monotonic_cst=[1, -1])
54+
gbdt.fit(X, y)
55+
56+
plot_partial_dependence(
57+
gbdt, X, features=[0, 1],
58+
feature_names=('First feature\nPositive constraint',
59+
'Second feature\nNegtive constraint'),
60+
line_kw={'linewidth': 4, 'label': 'constrained'},
61+
ax=disp.axes_)
62+
63+
for f_idx in (0, 1):
64+
disp.axes_[0, f_idx].plot(X[:, f_idx], y, 'o', alpha=.3, zorder=-1)
65+
disp.axes_[0, f_idx].set_ylim(-6, 6)
66+
67+
plt.legend()
68+
fig.suptitle("Monotonic constraints illustration")
69+
70+
plt.show()
Binary file not shown.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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# Monotonic Constraints\n\n\nThis example illustrates the effect of monotonic constraints on a gradient\nboosting estimator.\n\nWe build an artificial dataset where the target value is in general\npositively correlated with the first feature (with some random and\nnon-random variations), and in general negatively correlated with the second\nfeature.\n\nBy imposing a positive (increasing) or negative (decreasing) constraint on\nthe features during the learning process, the estimator is able to properly\nfollow the general trend instead of being subject to the variations.\n\nThis example was inspired by the `XGBoost documentation\n<https://xgboost.readthedocs.io/en/latest/tutorials/monotonic.html>`_.\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"from sklearn.experimental import enable_hist_gradient_boosting # noqa\nfrom sklearn.ensemble import HistGradientBoostingRegressor\nfrom sklearn.inspection import plot_partial_dependence\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n\nprint(__doc__)\n\nrng = np.random.RandomState(0)\n\nn_samples = 5000\nf_0 = rng.rand(n_samples) # positive correlation with y\nf_1 = rng.rand(n_samples) # negative correlation with y\nX = np.c_[f_0, f_1]\nnoise = rng.normal(loc=0.0, scale=0.01, size=n_samples)\ny = (5 * f_0 + np.sin(10 * np.pi * f_0) -\n 5 * f_1 - np.cos(10 * np.pi * f_1) +\n noise)\n\nfig, ax = plt.subplots()\n\n\n# Without any constraint\ngbdt = HistGradientBoostingRegressor()\ngbdt.fit(X, y)\ndisp = plot_partial_dependence(\n gbdt, X, features=[0, 1],\n line_kw={'linewidth': 4, 'label': 'unconstrained'},\n ax=ax)\n\n# With positive and negative constraints\ngbdt = HistGradientBoostingRegressor(monotonic_cst=[1, -1])\ngbdt.fit(X, y)\n\nplot_partial_dependence(\n gbdt, X, features=[0, 1],\n feature_names=('First feature\\nPositive constraint',\n 'Second feature\\nNegtive constraint'),\n line_kw={'linewidth': 4, 'label': 'constrained'},\n ax=disp.axes_)\n\nfor f_idx in (0, 1):\n disp.axes_[0, f_idx].plot(X[:, f_idx], y, 'o', alpha=.3, zorder=-1)\n disp.axes_[0, f_idx].set_ylim(-6, 6)\n\nplt.legend()\nfig.suptitle(\"Monotonic constraints illustration\")\n\nplt.show()"
30+
]
31+
}
32+
],
33+
"metadata": {
34+
"kernelspec": {
35+
"display_name": "Python 3",
36+
"language": "python",
37+
"name": "python3"
38+
},
39+
"language_info": {
40+
"codemirror_mode": {
41+
"name": "ipython",
42+
"version": 3
43+
},
44+
"file_extension": ".py",
45+
"mimetype": "text/x-python",
46+
"name": "python",
47+
"nbconvert_exporter": "python",
48+
"pygments_lexer": "ipython3",
49+
"version": "3.8.1"
50+
}
51+
},
52+
"nbformat": 4,
53+
"nbformat_minor": 0
54+
}
Binary file not shown.

dev/_downloads/scikit-learn-docs.pdf

121 KB
Binary file not shown.

dev/_images/iris.png

0 Bytes
-46 Bytes
-46 Bytes
-24 Bytes
-24 Bytes

0 commit comments

Comments
 (0)