Skip to content

Commit a6c17a7

Browse files
committed
Pushing the docs to dev/ for branch: master, commit 97185ecd3ec0bc8409781f3259b2436ba2a075bb
1 parent dda385f commit a6c17a7

File tree

1,225 files changed

+6427
-4416
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,225 files changed

+6427
-4416
lines changed
Binary file not shown.
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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# Advanced Plotting With Partial Dependence\n\nThe :func:`~sklearn.inspection.plot_partial_dependence` function returns a\n:class:`~sklearn.inspection.PartialDependenceDisplay` object that can be used\nfor plotting without needing to recalculate the partial dependence. In this\nexample, we show how to plot partial dependence plots and how to quickly\ncustomize the plot with the visualization API.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>See also `sphx_glr_auto_examples_plot_roc_curve_visualization_api.py`</p></div>\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\nimport matplotlib.pyplot as plt\nfrom sklearn.datasets import load_boston\nfrom sklearn.neural_network import MLPRegressor\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.inspection import plot_partial_dependence"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"Train models on the boston housing price dataset\n================================================\n\nFirst, we train a decision tree and a multi-layer perceptron on the boston\nhousing price dataset.\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"boston = load_boston()\nX, y = boston.data, boston.target\nfeature_names = boston.feature_names\n\ntree = DecisionTreeRegressor()\nmlp = make_pipeline(StandardScaler(),\n MLPRegressor(hidden_layer_sizes=(100, 100),\n tol=1e-2, max_iter=500, random_state=0))\ntree.fit(X, y)\nmlp.fit(X, y)"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"Plotting partial dependence for two features\n============================================\n\nWe plot partial dependence curves for features \"LSTAT\" and \"RM\" for\nthe decision tree. With two features,\n:func:`~sklearn.inspection.plot_partial_dependence` expects to plot two\ncurves. Here the plot function place a grid of two plots using the space\ndefined by `ax` .\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"fig, ax = plt.subplots(figsize=(12, 6))\nax.set_title(\"Decision Tree\")\ntree_disp = plot_partial_dependence(tree, X, [\"LSTAT\", \"RM\"],\n feature_names=feature_names, ax=ax)"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"The partial depdendence curves can be plotted for the multi-layer perceptron.\nIn this case, `line_kw` is passed to\n:func:`~sklearn.inspection.plot_partial_dependence` to change the color of\nthe curve.\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"fig, ax = plt.subplots(figsize=(12, 6))\nax.set_title(\"Multi-layer Perceptron\")\nmlp_disp = plot_partial_dependence(mlp, X, [\"LSTAT\", \"RM\"],\n feature_names=feature_names, ax=ax,\n line_kw={\"c\": \"red\"})"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"Plotting partial dependence of the two models together\n======================================================\n\nThe `tree_disp` and `mlp_disp`\n:class:`~sklearn.inspection.PartialDependenceDisplay` objects contain all the\ncomputed information needed to recreate the partial dependence curves. This\nmeans we can easily create additional plots without needing to recompute the\ncurves.\n\nOne way to plot the curves is to place them in the same figure, with the\ncurves of each model on each row. First, we create a figure with two axes\nwithin two rows and one column. The two axes are passed to the\n:func:`~sklearn.inspection.PartialDependenceDisplay.plot` functions of\n`tree_disp` and `mlp_disp`. The given axes will be used by the plotting\nfunction to draw the partial dependence. The resulting plot places the\ndecision tree partial dependence curves in the first row of the\nmulti-layer perceptron in the second row.\n\n"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {
97+
"collapsed": false
98+
},
99+
"outputs": [],
100+
"source": [
101+
"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10))\ntree_disp.plot(ax=ax1)\nax1.set_title(\"Decision Tree\")\nmlp_disp.plot(ax=ax2, line_kw={\"c\": \"red\"})\nax2.set_title(\"Multi-layer Perceptron\")"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"metadata": {},
107+
"source": [
108+
"Another way to compare the curves is to plot them on top of each other. Here,\nwe create a figure with one row and two columns. The axes are passed into the\n:func:`~sklearn.inspection.PartialDependenceDisplay.plot` function as a list,\nwhich will plot the partial dependence curves of each model on the same axes.\nThe length of the axes list must be equal to the number of plots drawn.\n\n"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"metadata": {
115+
"collapsed": false
116+
},
117+
"outputs": [],
118+
"source": [
119+
"# Sets this image as the thumbnail for sphinx gallery\n# sphinx_gallery_thumbnail_number = 4\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))\ntree_disp.plot(ax=[ax1, ax2], line_kw={\"label\": \"Decision Tree\"})\nmlp_disp.plot(ax=[ax1, ax2], line_kw={\"label\": \"Multi-layer Perceptron\",\n \"c\": \"red\"})\nax1.legend()\nax2.legend()"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"`tree_disp.axes_` is a numpy array container the axes used to draw the\npartial dependence plots. This can be passed to `mlp_disp` to have the same\naffect of drawing the plots on top of each other. Furthermore, the\n`mlp_disp.figure_` stores the figure, which allows for resizing the figure\nafter calling `plot`.\n\n"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {
133+
"collapsed": false
134+
},
135+
"outputs": [],
136+
"source": [
137+
"tree_disp.plot(line_kw={\"label\": \"Decision Tree\"})\nmlp_disp.plot(line_kw={\"label\": \"Multi-layer Perceptron\", \"c\": \"red\"},\n ax=tree_disp.axes_)\ntree_disp.figure_.set_size_inches(10, 6)\ntree_disp.axes_[0, 0].legend()\ntree_disp.axes_[0, 1].legend()\nplt.show()"
138+
]
139+
},
140+
{
141+
"cell_type": "markdown",
142+
"metadata": {},
143+
"source": [
144+
"Plotting partial dependence for one feature\n===========================================\n\nHere, we plot the partial dependence curves for a single feature, \"LSTAT\", on\nthe same axes. In this case, `tree_disp.axes_` is passed into the second\nplot function.\n\n"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {
151+
"collapsed": false
152+
},
153+
"outputs": [],
154+
"source": [
155+
"tree_disp = plot_partial_dependence(tree, X, [\"LSTAT\"],\n feature_names=feature_names)\nmlp_disp = plot_partial_dependence(mlp, X, [\"LSTAT\"],\n feature_names=feature_names,\n ax=tree_disp.axes_, line_kw={\"c\": \"red\"})"
156+
]
157+
}
158+
],
159+
"metadata": {
160+
"kernelspec": {
161+
"display_name": "Python 3",
162+
"language": "python",
163+
"name": "python3"
164+
},
165+
"language_info": {
166+
"codemirror_mode": {
167+
"name": "ipython",
168+
"version": 3
169+
},
170+
"file_extension": ".py",
171+
"mimetype": "text/x-python",
172+
"name": "python",
173+
"nbconvert_exporter": "python",
174+
"pygments_lexer": "ipython3",
175+
"version": "3.7.4"
176+
}
177+
},
178+
"nbformat": 4,
179+
"nbformat_minor": 0
180+
}

dev/_downloads/5a693c97e821586539ab9d250762742c/plot_partial_dependence.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
},
8181
"outputs": [],
8282
"source": [
83-
"print('Computing partial dependence plots...')\ntic = time()\n# We don't compute the 2-way PDP (5, 1) here, because it is a lot slower\n# with the brute method.\nfeatures = [0, 5, 1, 2]\nplot_partial_dependence(est, X_train, features, feature_names=names,\n n_jobs=3, grid_resolution=20)\nprint(\"done in {:.3f}s\".format(time() - tic))\nfig = plt.gcf()\nfig.suptitle('Partial dependence of house value on non-___location features\\n'\n 'for the California housing dataset, with MLPRegressor')\nfig.tight_layout(rect=[0, 0.03, 1, 0.95])"
83+
"print('Computing partial dependence plots...')\ntic = time()\n# We don't compute the 2-way PDP (5, 1) here, because it is a lot slower\n# with the brute method.\nfeatures = [0, 5, 1, 2]\nplot_partial_dependence(est, X_train, features, feature_names=names,\n n_jobs=3, grid_resolution=20)\nprint(\"done in {:.3f}s\".format(time() - tic))\nfig = plt.gcf()\nfig.suptitle('Partial dependence of house value on non-___location features\\n'\n 'for the California housing dataset, with MLPRegressor')\nfig.subplots_adjust(wspace=0.8, hspace=0.3)"
8484
]
8585
},
8686
{
@@ -116,7 +116,7 @@
116116
},
117117
"outputs": [],
118118
"source": [
119-
"print('Computing partial dependence plots...')\ntic = time()\nfeatures = [0, 5, 1, 2, (5, 1)]\nplot_partial_dependence(est, X_train, features, feature_names=names,\n n_jobs=3, grid_resolution=20)\nprint(\"done in {:.3f}s\".format(time() - tic))\nfig = plt.gcf()\nfig.suptitle('Partial dependence of house value on non-___location features\\n'\n 'for the California housing dataset, with Gradient Boosting')\nfig.tight_layout(rect=[0, 0.03, 1, 0.95])"
119+
"print('Computing partial dependence plots...')\ntic = time()\nfeatures = [0, 5, 1, 2, (5, 1)]\nplot_partial_dependence(est, X_train, features, feature_names=names,\n n_jobs=3, grid_resolution=20)\nprint(\"done in {:.3f}s\".format(time() - tic))\nfig = plt.gcf()\nfig.suptitle('Partial dependence of house value on non-___location features\\n'\n 'for the California housing dataset, with Gradient Boosting')\nfig.subplots_adjust(wspace=0.8, hspace=0.3)"
120120
]
121121
},
122122
{
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
"""
2+
=========================================
3+
Advanced Plotting With Partial Dependence
4+
=========================================
5+
The :func:`~sklearn.inspection.plot_partial_dependence` function returns a
6+
:class:`~sklearn.inspection.PartialDependenceDisplay` object that can be used
7+
for plotting without needing to recalculate the partial dependence. In this
8+
example, we show how to plot partial dependence plots and how to quickly
9+
customize the plot with the visualization API.
10+
11+
.. note::
12+
13+
See also :ref:`sphx_glr_auto_examples_plot_roc_curve_visualization_api.py`
14+
15+
"""
16+
print(__doc__)
17+
18+
import matplotlib.pyplot as plt
19+
from sklearn.datasets import load_boston
20+
from sklearn.neural_network import MLPRegressor
21+
from sklearn.preprocessing import StandardScaler
22+
from sklearn.pipeline import make_pipeline
23+
from sklearn.tree import DecisionTreeRegressor
24+
from sklearn.inspection import plot_partial_dependence
25+
26+
27+
##############################################################################
28+
# Train models on the boston housing price dataset
29+
# ================================================
30+
#
31+
# First, we train a decision tree and a multi-layer perceptron on the boston
32+
# housing price dataset.
33+
34+
boston = load_boston()
35+
X, y = boston.data, boston.target
36+
feature_names = boston.feature_names
37+
38+
tree = DecisionTreeRegressor()
39+
mlp = make_pipeline(StandardScaler(),
40+
MLPRegressor(hidden_layer_sizes=(100, 100),
41+
tol=1e-2, max_iter=500, random_state=0))
42+
tree.fit(X, y)
43+
mlp.fit(X, y)
44+
45+
46+
##############################################################################
47+
# Plotting partial dependence for two features
48+
# ============================================
49+
#
50+
# We plot partial dependence curves for features "LSTAT" and "RM" for
51+
# the decision tree. With two features,
52+
# :func:`~sklearn.inspection.plot_partial_dependence` expects to plot two
53+
# curves. Here the plot function place a grid of two plots using the space
54+
# defined by `ax` .
55+
fig, ax = plt.subplots(figsize=(12, 6))
56+
ax.set_title("Decision Tree")
57+
tree_disp = plot_partial_dependence(tree, X, ["LSTAT", "RM"],
58+
feature_names=feature_names, ax=ax)
59+
60+
##############################################################################
61+
# The partial depdendence curves can be plotted for the multi-layer perceptron.
62+
# In this case, `line_kw` is passed to
63+
# :func:`~sklearn.inspection.plot_partial_dependence` to change the color of
64+
# the curve.
65+
fig, ax = plt.subplots(figsize=(12, 6))
66+
ax.set_title("Multi-layer Perceptron")
67+
mlp_disp = plot_partial_dependence(mlp, X, ["LSTAT", "RM"],
68+
feature_names=feature_names, ax=ax,
69+
line_kw={"c": "red"})
70+
71+
##############################################################################
72+
# Plotting partial dependence of the two models together
73+
# ======================================================
74+
#
75+
# The `tree_disp` and `mlp_disp`
76+
# :class:`~sklearn.inspection.PartialDependenceDisplay` objects contain all the
77+
# computed information needed to recreate the partial dependence curves. This
78+
# means we can easily create additional plots without needing to recompute the
79+
# curves.
80+
#
81+
# One way to plot the curves is to place them in the same figure, with the
82+
# curves of each model on each row. First, we create a figure with two axes
83+
# within two rows and one column. The two axes are passed to the
84+
# :func:`~sklearn.inspection.PartialDependenceDisplay.plot` functions of
85+
# `tree_disp` and `mlp_disp`. The given axes will be used by the plotting
86+
# function to draw the partial dependence. The resulting plot places the
87+
# decision tree partial dependence curves in the first row of the
88+
# multi-layer perceptron in the second row.
89+
90+
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10))
91+
tree_disp.plot(ax=ax1)
92+
ax1.set_title("Decision Tree")
93+
mlp_disp.plot(ax=ax2, line_kw={"c": "red"})
94+
ax2.set_title("Multi-layer Perceptron")
95+
96+
##############################################################################
97+
# Another way to compare the curves is to plot them on top of each other. Here,
98+
# we create a figure with one row and two columns. The axes are passed into the
99+
# :func:`~sklearn.inspection.PartialDependenceDisplay.plot` function as a list,
100+
# which will plot the partial dependence curves of each model on the same axes.
101+
# The length of the axes list must be equal to the number of plots drawn.
102+
103+
# Sets this image as the thumbnail for sphinx gallery
104+
# sphinx_gallery_thumbnail_number = 4
105+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))
106+
tree_disp.plot(ax=[ax1, ax2], line_kw={"label": "Decision Tree"})
107+
mlp_disp.plot(ax=[ax1, ax2], line_kw={"label": "Multi-layer Perceptron",
108+
"c": "red"})
109+
ax1.legend()
110+
ax2.legend()
111+
112+
##############################################################################
113+
# `tree_disp.axes_` is a numpy array container the axes used to draw the
114+
# partial dependence plots. This can be passed to `mlp_disp` to have the same
115+
# affect of drawing the plots on top of each other. Furthermore, the
116+
# `mlp_disp.figure_` stores the figure, which allows for resizing the figure
117+
# after calling `plot`.
118+
119+
tree_disp.plot(line_kw={"label": "Decision Tree"})
120+
mlp_disp.plot(line_kw={"label": "Multi-layer Perceptron", "c": "red"},
121+
ax=tree_disp.axes_)
122+
tree_disp.figure_.set_size_inches(10, 6)
123+
tree_disp.axes_[0, 0].legend()
124+
tree_disp.axes_[0, 1].legend()
125+
plt.show()
126+
127+
128+
##############################################################################
129+
# Plotting partial dependence for one feature
130+
# ===========================================
131+
#
132+
# Here, we plot the partial dependence curves for a single feature, "LSTAT", on
133+
# the same axes. In this case, `tree_disp.axes_` is passed into the second
134+
# plot function.
135+
tree_disp = plot_partial_dependence(tree, X, ["LSTAT"],
136+
feature_names=feature_names)
137+
mlp_disp = plot_partial_dependence(mlp, X, ["LSTAT"],
138+
feature_names=feature_names,
139+
ax=tree_disp.axes_, line_kw={"c": "red"})
Binary file not shown.

dev/_downloads/fa25d310c75e4ff65e62ab2cd8fdcef4/plot_partial_dependence.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
fig = plt.gcf()
112112
fig.suptitle('Partial dependence of house value on non-___location features\n'
113113
'for the California housing dataset, with MLPRegressor')
114-
fig.tight_layout(rect=[0, 0.03, 1, 0.95])
114+
fig.subplots_adjust(wspace=0.8, hspace=0.3)
115115

116116
##############################################################################
117117
# Partial Dependence computation for Gradient Boosting
@@ -150,7 +150,8 @@
150150
fig = plt.gcf()
151151
fig.suptitle('Partial dependence of house value on non-___location features\n'
152152
'for the California housing dataset, with Gradient Boosting')
153-
fig.tight_layout(rect=[0, 0.03, 1, 0.95])
153+
fig.subplots_adjust(wspace=0.8, hspace=0.3)
154+
154155

155156
##############################################################################
156157
# Analysis of the plots

dev/_downloads/scikit-learn-docs.pdf

218 KB
Binary file not shown.

dev/_images/iris.png

0 Bytes
-303 Bytes
-303 Bytes

0 commit comments

Comments
 (0)