Skip to content

Commit 895c989

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 696ca3de57d210ffa1fdf14c50b7d1bd6a482396
1 parent 2362891 commit 895c989

File tree

1,203 files changed

+4274
-4117
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,203 files changed

+4274
-4117
lines changed
Binary file not shown.

dev/_downloads/21b82d82985712b5de6347f382c77c86/plot_partial_dependence.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
},
4545
"outputs": [],
4646
"source": [
47-
"import pandas as pd\nfrom sklearn.datasets import fetch_california_housing\nfrom sklearn.model_selection import train_test_split\n\ncal_housing = fetch_california_housing()\nX = pd.DataFrame(cal_housing.data, columns=cal_housing.feature_names)\ny = cal_housing.target\n\ny -= y.mean()\n\nX_train, X_test, y_train, y_test = train_test_split(\n X, y, test_size=0.1, random_state=0\n)"
47+
"import pandas as pd\nfrom sklearn.datasets import fetch_california_housing\nfrom sklearn.model_selection import train_test_split\n\ncal_housing = fetch_california_housing()\nX = pd.DataFrame(cal_housing.data, columns=cal_housing.feature_names)\ny = cal_housing.target\n\ny -= y.mean()\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)"
4848
]
4949
},
5050
{
@@ -62,7 +62,7 @@
6262
},
6363
"outputs": [],
6464
"source": [
65-
"from time import time\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import QuantileTransformer\nfrom sklearn.neural_network import MLPRegressor\n\nprint(\"Training MLPRegressor...\")\ntic = time()\nest = make_pipeline(QuantileTransformer(),\n MLPRegressor(hidden_layer_sizes=(50, 50),\n learning_rate_init=0.01,\n early_stopping=True))\nest.fit(X_train, y_train)\nprint(f\"done in {time() - tic:.3f}s\")\nprint(f\"Test R2 score: {est.score(X_test, y_test):.2f}\")"
65+
"from time import time\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import QuantileTransformer\nfrom sklearn.neural_network import MLPRegressor\n\nprint(\"Training MLPRegressor...\")\ntic = time()\nest = make_pipeline(\n QuantileTransformer(),\n MLPRegressor(\n hidden_layer_sizes=(50, 50), learning_rate_init=0.01, early_stopping=True\n ),\n)\nest.fit(X_train, y_train)\nprint(f\"done in {time() - tic:.3f}s\")\nprint(f\"Test R2 score: {est.score(X_test, y_test):.2f}\")"
6666
]
6767
},
6868
{
@@ -80,7 +80,7 @@
8080
},
8181
"outputs": [],
8282
"source": [
83-
"import matplotlib.pyplot as plt\nfrom sklearn.inspection import partial_dependence\nfrom sklearn.inspection import plot_partial_dependence\n\nprint('Computing partial dependence plots...')\ntic = time()\nfeatures = ['MedInc', 'AveOccup', 'HouseAge', 'AveRooms']\ndisplay = plot_partial_dependence(\n est, X_train, features, kind=\"both\", subsample=50,\n n_jobs=3, grid_resolution=20, random_state=0\n)\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n 'Partial dependence of house value on non-___location features\\n'\n 'for the California housing dataset, with MLPRegressor'\n)\ndisplay.figure_.subplots_adjust(hspace=0.3)"
83+
"import matplotlib.pyplot as plt\nfrom sklearn.inspection import partial_dependence\nfrom sklearn.inspection import plot_partial_dependence\n\nprint(\"Computing partial dependence plots...\")\ntic = time()\nfeatures = [\"MedInc\", \"AveOccup\", \"HouseAge\", \"AveRooms\"]\ndisplay = plot_partial_dependence(\n est,\n X_train,\n features,\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)\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n \"Partial dependence of house value on non-___location features\\n\"\n \"for the California housing dataset, with MLPRegressor\"\n)\ndisplay.figure_.subplots_adjust(hspace=0.3)"
8484
]
8585
},
8686
{
@@ -116,7 +116,7 @@
116116
},
117117
"outputs": [],
118118
"source": [
119-
"print('Computing partial dependence plots...')\ntic = time()\ndisplay = plot_partial_dependence(\n est, X_train, features, kind=\"both\", subsample=50,\n n_jobs=3, grid_resolution=20, random_state=0\n)\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n 'Partial dependence of house value on non-___location features\\n'\n 'for the California housing dataset, with Gradient Boosting'\n)\ndisplay.figure_.subplots_adjust(wspace=0.4, hspace=0.3)"
119+
"print(\"Computing partial dependence plots...\")\ntic = time()\ndisplay = plot_partial_dependence(\n est,\n X_train,\n features,\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)\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n \"Partial dependence of house value on non-___location features\\n\"\n \"for the California housing dataset, with Gradient Boosting\"\n)\ndisplay.figure_.subplots_adjust(wspace=0.4, hspace=0.3)"
120120
]
121121
},
122122
{
@@ -141,7 +141,7 @@
141141
},
142142
"outputs": [],
143143
"source": [
144-
"features = ['AveOccup', 'HouseAge', ('AveOccup', 'HouseAge')]\nprint('Computing partial dependence plots...')\ntic = time()\n_, ax = plt.subplots(ncols=3, figsize=(9, 4))\ndisplay = plot_partial_dependence(\n est, X_train, features, kind='average', n_jobs=3, grid_resolution=20,\n ax=ax,\n)\nprint(f\"done in {time() - tic:.3f}s\")\ndisplay.figure_.suptitle(\n 'Partial dependence of house value on non-___location features\\n'\n 'for the California housing dataset, with Gradient Boosting'\n)\ndisplay.figure_.subplots_adjust(wspace=0.4, hspace=0.3)"
144+
"features = [\"AveOccup\", \"HouseAge\", (\"AveOccup\", \"HouseAge\")]\nprint(\"Computing partial dependence plots...\")\ntic = time()\n_, ax = plt.subplots(ncols=3, figsize=(9, 4))\ndisplay = plot_partial_dependence(\n est,\n X_train,\n features,\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 on non-___location features\\n\"\n \"for the California housing dataset, with Gradient Boosting\"\n)\ndisplay.figure_.subplots_adjust(wspace=0.4, hspace=0.3)"
145145
]
146146
},
147147
{
@@ -159,7 +159,7 @@
159159
},
160160
"outputs": [],
161161
"source": [
162-
"import numpy as np\nfrom mpl_toolkits.mplot3d import Axes3D\nfig = plt.figure()\n\nfeatures = ('AveOccup', 'HouseAge')\npdp = partial_dependence(\n est, X_train, features=features, kind='average', grid_resolution=20\n)\nXX, YY = np.meshgrid(pdp[\"values\"][0], pdp[\"values\"][1])\nZ = pdp.average[0].T\nax = Axes3D(fig)\nsurf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1,\n cmap=plt.cm.BuPu, edgecolor='k')\nax.set_xlabel(features[0])\nax.set_ylabel(features[1])\nax.set_zlabel('Partial dependence')\n# pretty init view\nax.view_init(elev=22, azim=122)\nplt.colorbar(surf)\nplt.suptitle('Partial dependence of house value on median\\n'\n 'age and average occupancy, with Gradient Boosting')\nplt.subplots_adjust(top=0.9)\nplt.show()"
162+
"import numpy as np\nfrom mpl_toolkits.mplot3d import Axes3D\n\nfig = plt.figure()\n\nfeatures = (\"AveOccup\", \"HouseAge\")\npdp = partial_dependence(\n est, X_train, features=features, kind=\"average\", grid_resolution=20\n)\nXX, YY = np.meshgrid(pdp[\"values\"][0], pdp[\"values\"][1])\nZ = pdp.average[0].T\nax = Axes3D(fig)\nfig.add_axes(ax)\nsurf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1, cmap=plt.cm.BuPu, edgecolor=\"k\")\nax.set_xlabel(features[0])\nax.set_ylabel(features[1])\nax.set_zlabel(\"Partial dependence\")\n# pretty init view\nax.view_init(elev=22, azim=122)\nplt.colorbar(surf)\nplt.suptitle(\n \"Partial dependence of house value on median\\n\"\n \"age and average occupancy, with Gradient Boosting\"\n)\nplt.subplots_adjust(top=0.9)\nplt.show()"
163163
]
164164
}
165165
],
Binary file not shown.

dev/_downloads/bcd609cfe29c9da1f51c848e18b89c76/plot_partial_dependence.py

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@
5353

5454
y -= y.mean()
5555

56-
X_train, X_test, y_train, y_test = train_test_split(
57-
X, y, test_size=0.1, random_state=0
58-
)
56+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
5957

6058
# %%
6159
# 1-way partial dependence with different models
@@ -80,10 +78,12 @@
8078

8179
print("Training MLPRegressor...")
8280
tic = time()
83-
est = make_pipeline(QuantileTransformer(),
84-
MLPRegressor(hidden_layer_sizes=(50, 50),
85-
learning_rate_init=0.01,
86-
early_stopping=True))
81+
est = make_pipeline(
82+
QuantileTransformer(),
83+
MLPRegressor(
84+
hidden_layer_sizes=(50, 50), learning_rate_init=0.01, early_stopping=True
85+
),
86+
)
8787
est.fit(X_train, y_train)
8888
print(f"done in {time() - tic:.3f}s")
8989
print(f"Test R2 score: {est.score(X_test, y_test):.2f}")
@@ -113,17 +113,25 @@
113113
from sklearn.inspection import partial_dependence
114114
from sklearn.inspection import plot_partial_dependence
115115

116-
print('Computing partial dependence plots...')
116+
print("Computing partial dependence plots...")
117117
tic = time()
118-
features = ['MedInc', 'AveOccup', 'HouseAge', 'AveRooms']
118+
features = ["MedInc", "AveOccup", "HouseAge", "AveRooms"]
119119
display = plot_partial_dependence(
120-
est, X_train, features, kind="both", subsample=50,
121-
n_jobs=3, grid_resolution=20, random_state=0
120+
est,
121+
X_train,
122+
features,
123+
kind="both",
124+
subsample=50,
125+
n_jobs=3,
126+
grid_resolution=20,
127+
random_state=0,
128+
ice_lines_kw={"color": "tab:blue", "alpha": 0.2, "linewidth": 0.5},
129+
pd_line_kw={"color": "tab:orange", "linestyle": "--"},
122130
)
123131
print(f"done in {time() - tic:.3f}s")
124132
display.figure_.suptitle(
125-
'Partial dependence of house value on non-___location features\n'
126-
'for the California housing dataset, with MLPRegressor'
133+
"Partial dependence of house value on non-___location features\n"
134+
"for the California housing dataset, with MLPRegressor"
127135
)
128136
display.figure_.subplots_adjust(hspace=0.3)
129137

@@ -156,16 +164,24 @@
156164
# We will plot the partial dependence, both individual (ICE) and averaged one
157165
# (PDP). We limit to only 50 ICE curves to not overcrowd the plot.
158166

159-
print('Computing partial dependence plots...')
167+
print("Computing partial dependence plots...")
160168
tic = time()
161169
display = plot_partial_dependence(
162-
est, X_train, features, kind="both", subsample=50,
163-
n_jobs=3, grid_resolution=20, random_state=0
170+
est,
171+
X_train,
172+
features,
173+
kind="both",
174+
subsample=50,
175+
n_jobs=3,
176+
grid_resolution=20,
177+
random_state=0,
178+
ice_lines_kw={"color": "tab:blue", "alpha": 0.2, "linewidth": 0.5},
179+
pd_line_kw={"color": "tab:orange", "linestyle": "--"},
164180
)
165181
print(f"done in {time() - tic:.3f}s")
166182
display.figure_.suptitle(
167-
'Partial dependence of house value on non-___location features\n'
168-
'for the California housing dataset, with Gradient Boosting'
183+
"Partial dependence of house value on non-___location features\n"
184+
"for the California housing dataset, with Gradient Boosting"
169185
)
170186
display.figure_.subplots_adjust(wspace=0.4, hspace=0.3)
171187

@@ -209,18 +225,23 @@
209225
# the tree-based algorithm, when only PDPs are requested, they can be computed
210226
# on an efficient way using the `'recursion'` method.
211227

212-
features = ['AveOccup', 'HouseAge', ('AveOccup', 'HouseAge')]
213-
print('Computing partial dependence plots...')
228+
features = ["AveOccup", "HouseAge", ("AveOccup", "HouseAge")]
229+
print("Computing partial dependence plots...")
214230
tic = time()
215231
_, ax = plt.subplots(ncols=3, figsize=(9, 4))
216232
display = plot_partial_dependence(
217-
est, X_train, features, kind='average', n_jobs=3, grid_resolution=20,
233+
est,
234+
X_train,
235+
features,
236+
kind="average",
237+
n_jobs=3,
238+
grid_resolution=20,
218239
ax=ax,
219240
)
220241
print(f"done in {time() - tic:.3f}s")
221242
display.figure_.suptitle(
222-
'Partial dependence of house value on non-___location features\n'
223-
'for the California housing dataset, with Gradient Boosting'
243+
"Partial dependence of house value on non-___location features\n"
244+
"for the California housing dataset, with Gradient Boosting"
224245
)
225246
display.figure_.subplots_adjust(wspace=0.4, hspace=0.3)
226247

@@ -240,24 +261,27 @@
240261

241262
import numpy as np
242263
from mpl_toolkits.mplot3d import Axes3D
264+
243265
fig = plt.figure()
244266

245-
features = ('AveOccup', 'HouseAge')
267+
features = ("AveOccup", "HouseAge")
246268
pdp = partial_dependence(
247-
est, X_train, features=features, kind='average', grid_resolution=20
269+
est, X_train, features=features, kind="average", grid_resolution=20
248270
)
249271
XX, YY = np.meshgrid(pdp["values"][0], pdp["values"][1])
250272
Z = pdp.average[0].T
251273
ax = Axes3D(fig)
252-
surf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1,
253-
cmap=plt.cm.BuPu, edgecolor='k')
274+
fig.add_axes(ax)
275+
surf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1, cmap=plt.cm.BuPu, edgecolor="k")
254276
ax.set_xlabel(features[0])
255277
ax.set_ylabel(features[1])
256-
ax.set_zlabel('Partial dependence')
278+
ax.set_zlabel("Partial dependence")
257279
# pretty init view
258280
ax.view_init(elev=22, azim=122)
259281
plt.colorbar(surf)
260-
plt.suptitle('Partial dependence of house value on median\n'
261-
'age and average occupancy, with Gradient Boosting')
282+
plt.suptitle(
283+
"Partial dependence of house value on median\n"
284+
"age and average occupancy, with Gradient Boosting"
285+
)
262286
plt.subplots_adjust(top=0.9)
263287
plt.show()

dev/_downloads/scikit-learn-docs.zip

7.22 KB
Binary file not shown.

0 commit comments

Comments
 (0)