Skip to content

Commit c1aa059

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 5ceb8a6a031ddff26a7ede413db1b53edb64166a
1 parent e74ff1d commit c1aa059

File tree

1,269 files changed

+4905
-4366
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,269 files changed

+4905
-4366
lines changed
Binary file not shown.

dev/_downloads/21b82d82985712b5de6347f382c77c86/plot_partial_dependence.ipynb

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,61 @@
137137
"cell_type": "markdown",
138138
"metadata": {},
139139
"source": [
140-
"The two-way partial dependence plot shows the dependence of median house\nprice on joint values of house age and average occupants per household. We\ncan clearly see an interaction between the two features: for an average\noccupancy greater than two, the house price is nearly independent of the\nhouse age, whereas for values less than two there is a strong dependence on\nage.\n\n## 3D interaction plots\n\nLet's make the same partial dependence plot for the 2 features interaction,\nthis time in 3 dimensions.\n\n"
140+
"The two-way partial dependence plot shows the dependence of median house\nprice on joint values of house age and average occupants per household. We\ncan clearly see an interaction between the two features: for an average\noccupancy greater than two, the house price is nearly independent of the\nhouse age, whereas for values less than two there is a strong dependence on\nage.\n\n### Interaction constraints\n\nThe histogram gradient boosters have an interesting option to constrain\npossible interactions among features. In the following, we do not allow any\ninteractions and thus render the model as a version of a tree-based boosted\ngeneralized additive model (GAM). This makes the model more interpretable\nas the effect of each feature can be investigated independently of all others.\n\nWe train the :class:`~sklearn.ensemble.HistGradientBoostingRegressor` again,\nnow with `interaction_cst`, where we pass for each feature a list containing\nonly its own index, e.g. `[[0], [1], [2], ..]`.\n\n"
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": null,
146+
"metadata": {
147+
"collapsed": false
148+
},
149+
"outputs": [],
150+
"source": [
151+
"print(\"Training interaction constraint HistGradientBoostingRegressor...\")\ntic = time()\nest_no_interactions = HistGradientBoostingRegressor(\n interaction_cst=[[i] for i in range(X_train.shape[1])]\n)\nest_no_interactions.fit(X_train, y_train)\nprint(f\"done in {time() - tic:.3f}s\")"
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"The easiest way to show the effect of forbidden interactions is again the\nICE plots.\n\n"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": null,
164+
"metadata": {
165+
"collapsed": false
166+
},
167+
"outputs": [],
168+
"source": [
169+
"print(\"Computing partial dependence plots...\")\ntic = time()\ndisplay = PartialDependenceDisplay.from_estimator(\n est_no_interactions,\n X_train,\n [\"MedInc\", \"AveOccup\", \"HouseAge\", \"AveRooms\"],\n kind=\"both\",\n subsample=50,\n n_jobs=3,\n grid_resolution=20,\n random_state=0,\n ice_lines_kw={\"color\": \"tab:blue\", \"alpha\": 0.2, \"linewidth\": 0.5},\n pd_line_kw={\"color\": \"tab:orange\", \"linestyle\": \"--\"},\n)\n\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n \"Partial dependence of house value with Gradient Boosting\\n\"\n \"and no interactions allowed\"\n)\ndisplay.figure_.subplots_adjust(wspace=0.4, hspace=0.3)"
170+
]
171+
},
172+
{
173+
"cell_type": "markdown",
174+
"metadata": {},
175+
"source": [
176+
"All 4 plots have parallel ICE lines meaning there is no interaction in the\nmodel.\nLet us also have a look at the corresponding 2D-plot.\n\n"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"metadata": {
183+
"collapsed": false
184+
},
185+
"outputs": [],
186+
"source": [
187+
"print(\"Computing partial dependence plots...\")\ntic = time()\n_, ax = plt.subplots(ncols=3, figsize=(9, 4))\ndisplay = PartialDependenceDisplay.from_estimator(\n est_no_interactions,\n X_train,\n [\"AveOccup\", \"HouseAge\", (\"AveOccup\", \"HouseAge\")],\n kind=\"average\",\n n_jobs=3,\n grid_resolution=20,\n ax=ax,\n)\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n \"Partial dependence of house value with Gradient Boosting\\n\"\n \"and no interactions allowed\"\n)\ndisplay.figure_.subplots_adjust(wspace=0.4, hspace=0.3)"
188+
]
189+
},
190+
{
191+
"cell_type": "markdown",
192+
"metadata": {},
193+
"source": [
194+
"Although the 2D-plot shows much less interaction compared with the 2D-plot\nfrom above, it is much harder to come to the conclusion that there is no\ninteraction at all. This might be a cause of the discrete predictions of\ntrees in combination with numerically precision of partial dependence.\nWe also observe that the univariate dependence plots have slightly changed\nas the model tries to compensate for the forbidden interactions.\n\n## 3D interaction plots\n\nLet's make the same partial dependence plot for the 2 features interaction,\nthis time in 3 dimensions.\n\n"
141195
]
142196
},
143197
{
Binary file not shown.

dev/_downloads/bcd609cfe29c9da1f51c848e18b89c76/plot_partial_dependence.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,85 @@
255255
# house age, whereas for values less than two there is a strong dependence on
256256
# age.
257257
#
258+
# Interaction constraints
259+
# .......................
260+
#
261+
# The histogram gradient boosters have an interesting option to constrain
262+
# possible interactions among features. In the following, we do not allow any
263+
# interactions and thus render the model as a version of a tree-based boosted
264+
# generalized additive model (GAM). This makes the model more interpretable
265+
# as the effect of each feature can be investigated independently of all others.
266+
#
267+
# We train the :class:`~sklearn.ensemble.HistGradientBoostingRegressor` again,
268+
# now with `interaction_cst`, where we pass for each feature a list containing
269+
# only its own index, e.g. `[[0], [1], [2], ..]`.
270+
271+
print("Training interaction constraint HistGradientBoostingRegressor...")
272+
tic = time()
273+
est_no_interactions = HistGradientBoostingRegressor(
274+
interaction_cst=[[i] for i in range(X_train.shape[1])]
275+
)
276+
est_no_interactions.fit(X_train, y_train)
277+
print(f"done in {time() - tic:.3f}s")
278+
279+
# %%
280+
# The easiest way to show the effect of forbidden interactions is again the
281+
# ICE plots.
282+
283+
print("Computing partial dependence plots...")
284+
tic = time()
285+
display = PartialDependenceDisplay.from_estimator(
286+
est_no_interactions,
287+
X_train,
288+
["MedInc", "AveOccup", "HouseAge", "AveRooms"],
289+
kind="both",
290+
subsample=50,
291+
n_jobs=3,
292+
grid_resolution=20,
293+
random_state=0,
294+
ice_lines_kw={"color": "tab:blue", "alpha": 0.2, "linewidth": 0.5},
295+
pd_line_kw={"color": "tab:orange", "linestyle": "--"},
296+
)
297+
298+
print(f"done in {time() - tic:.3f}s")
299+
display.figure_.suptitle(
300+
"Partial dependence of house value with Gradient Boosting\n"
301+
"and no interactions allowed"
302+
)
303+
display.figure_.subplots_adjust(wspace=0.4, hspace=0.3)
304+
305+
# %%
306+
# All 4 plots have parallel ICE lines meaning there is no interaction in the
307+
# model.
308+
# Let us also have a look at the corresponding 2D-plot.
309+
310+
print("Computing partial dependence plots...")
311+
tic = time()
312+
_, ax = plt.subplots(ncols=3, figsize=(9, 4))
313+
display = PartialDependenceDisplay.from_estimator(
314+
est_no_interactions,
315+
X_train,
316+
["AveOccup", "HouseAge", ("AveOccup", "HouseAge")],
317+
kind="average",
318+
n_jobs=3,
319+
grid_resolution=20,
320+
ax=ax,
321+
)
322+
print(f"done in {time() - tic:.3f}s")
323+
display.figure_.suptitle(
324+
"Partial dependence of house value with Gradient Boosting\n"
325+
"and no interactions allowed"
326+
)
327+
display.figure_.subplots_adjust(wspace=0.4, hspace=0.3)
328+
329+
# %%
330+
# Although the 2D-plot shows much less interaction compared with the 2D-plot
331+
# from above, it is much harder to come to the conclusion that there is no
332+
# interaction at all. This might be a cause of the discrete predictions of
333+
# trees in combination with numerically precision of partial dependence.
334+
# We also observe that the univariate dependence plots have slightly changed
335+
# as the model tries to compensate for the forbidden interactions.
336+
#
258337
# 3D interaction plots
259338
# --------------------
260339
#

dev/_downloads/scikit-learn-docs.zip

137 KB
Binary file not shown.
-156 Bytes
146 Bytes
149 Bytes
291 Bytes
-4 Bytes

0 commit comments

Comments
 (0)