Skip to content

Commit a4fad13

Browse files
author
ArturoAmorQ
committed
Synch notebooks
1 parent 108ff52 commit a4fad13

5 files changed

Lines changed: 258 additions & 125 deletions

File tree

notebooks/dimred_components.ipynb

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"source": [
6161
"from skrub import TableReport\n",
6262
"\n",
63-
"TableReport(X)"
63+
"TableReport(X, verbose=0)"
6464
]
6565
},
6666
{
@@ -171,7 +171,9 @@
171171
"source": [
172172
"pipe_90 = make_pipeline(StandardScaler(), PCA(n_components=0.90))\n",
173173
"pipe_90.fit(X)\n",
174-
"print(f\"n_components_ for 90% threshold: {pipe_90.named_steps['pca'].n_components_}\")"
174+
"print(\n",
175+
" f\"n_components_ for 90% threshold: {pipe_90.named_steps['pca'].n_components_}\"\n",
176+
")"
175177
]
176178
},
177179
{
@@ -204,7 +206,9 @@
204206
" X_split, _ = train_test_split(X, train_size=0.5, random_state=random_state)\n",
205207
" pipe_split = make_pipeline(StandardScaler(), PCA())\n",
206208
" pipe_split.fit(X_split)\n",
207-
" split_explained.append(pipe_split.named_steps[\"pca\"].explained_variance_ratio_)"
209+
" split_explained.append(\n",
210+
" pipe_split.named_steps[\"pca\"].explained_variance_ratio_\n",
211+
" )"
208212
]
209213
},
210214
{
@@ -216,9 +220,13 @@
216220
"fig, ax = plt.subplots(figsize=(8, 4))\n",
217221
"\n",
218222
"for ev in split_explained:\n",
219-
" ax.plot(np.arange(1, len(ev) + 1), np.cumsum(ev), color=\"tab:blue\", alpha=0.2)\n",
223+
" ax.plot(\n",
224+
" np.arange(1, len(ev) + 1), np.cumsum(ev), color=\"tab:blue\", alpha=0.2\n",
225+
" )\n",
220226
"\n",
221-
"ax.plot(components, cumulative, color=\"tab:blue\", linewidth=2, label=\"Full dataset\")\n",
227+
"ax.plot(\n",
228+
" components, cumulative, color=\"tab:blue\", linewidth=2, label=\"Full dataset\"\n",
229+
")\n",
222230
"ax.axhline(0.90, color=\"tab:orange\", linestyle=\"--\", label=\"90%\")\n",
223231
"ax.axhline(0.95, color=\"tab:red\", linestyle=\"--\", label=\"95%\")\n",
224232
"ax.set_xlabel(\"Number of components\")\n",
@@ -318,7 +326,9 @@
318326
"for ev in split_explained:\n",
319327
" ax.plot(np.arange(1, len(ev) + 1), ev, color=\"tab:blue\", alpha=0.2)\n",
320328
"\n",
321-
"ax.plot(components, explained, color=\"tab:blue\", linewidth=2, label=\"Full dataset\")\n",
329+
"ax.plot(\n",
330+
" components, explained, color=\"tab:blue\", linewidth=2, label=\"Full dataset\"\n",
331+
")\n",
322332
"ax.axhline(\n",
323333
" kaiser_threshold,\n",
324334
" color=\"tab:red\",\n",
@@ -424,15 +434,20 @@
424434
"for ax, n_components, label in zip(\n",
425435
" axes,\n",
426436
" [kaiser_n, threshold_90],\n",
427-
" [f\"Kaiser ({kaiser_n} components)\", f\"90% threshold ({threshold_90} components)\"],\n",
437+
" [\n",
438+
" f\"Kaiser ({kaiser_n} components)\",\n",
439+
" f\"90% threshold ({threshold_90} components)\",\n",
440+
" ],\n",
428441
"):\n",
429442
" pipe_km = make_pipeline(\n",
430443
" StandardScaler(),\n",
431444
" PCA(n_components=n_components),\n",
432445
" KMeans(random_state=0),\n",
433446
" )\n",
434447
" for random_state in range(1, 11):\n",
435-
" X_sub, _ = train_test_split(X, train_size=0.5, random_state=random_state)\n",
448+
" X_sub, _ = train_test_split(\n",
449+
" X, train_size=0.5, random_state=random_state\n",
450+
" )\n",
436451
" scores = []\n",
437452
" for k in n_clusters_range:\n",
438453
" pipe_km[-1].set_params(n_clusters=k)\n",

notebooks/dimred_ex_01.ipynb

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Solution for Exercise M8.01\n",
7+
"# Exercise M8.01\n",
88
"\n",
99
"In the Chapter on Linear Models we saw that feature engineering using\n",
1010
"`PolynomialFeatures` can give a linear model the flexibility to capture\n",
@@ -111,10 +111,7 @@
111111
"metadata": {},
112112
"source": [
113113
"Build a `grid_search_results` DataFrame from the attribute `cv_results_`\n",
114-
"keeping only the columns listed in `results_columns` below. Add a\n",
115-
"`\"mean_test_error\"` column as the negative of `\"mean_test_score\"`, drop\n",
116-
"`\"mean_test_score\"`, and rename `\"param_pca__n_components\"` to\n",
117-
"`\"n_components\"`."
114+
"keeping only the columns listed in `results_columns` below."
118115
]
119116
},
120117
{
@@ -160,30 +157,53 @@
160157
"source": [
161158
"import plotly.express as px\n",
162159
"\n",
163-
"labels = {\n",
164-
" \"mean_fit_time\": \"CV fit time (s)\",\n",
165-
" \"mean_test_error\": \"CV score (MAE)\",\n",
166-
"}\n",
167-
"grid_search_results[\"n_components\"] = grid_search_results[\"n_components\"].fillna(\"None\")\n",
168-
"fig = px.scatter(\n",
169-
" grid_search_results,\n",
170-
" x=\"mean_fit_time\",\n",
171-
" y=\"mean_test_error\",\n",
172-
" error_x=\"std_fit_time\",\n",
173-
" error_y=\"std_test_score\",\n",
174-
" hover_data=grid_search_results.columns,\n",
175-
" labels=labels,\n",
176-
")\n",
177-
"fig.update_layout(\n",
178-
" title={\n",
179-
" \"text\": \"Trade-off between fit time and mean test score\",\n",
180-
" \"y\": 0.95,\n",
181-
" \"x\": 0.5,\n",
182-
" \"xanchor\": \"center\",\n",
183-
" \"yanchor\": \"top\",\n",
160+
"\n",
161+
"def plot_grid_search_results(grid_search_results):\n",
162+
" grid_search_results[\"mean_test_error\"] = -grid_search_results[\n",
163+
" \"mean_test_score\"\n",
164+
" ]\n",
165+
" grid_search_results = (\n",
166+
" grid_search_results.drop(columns=[\"mean_test_score\"])\n",
167+
" .rename(columns={\"param_\" + param_name: \"n_components\"})\n",
168+
" .round(2)\n",
169+
" )\n",
170+
" grid_search_results.sort_values(\"mean_test_error\", ascending=False)\n",
171+
"\n",
172+
" labels = {\n",
173+
" \"mean_fit_time\": \"CV fit time (s)\",\n",
174+
" \"mean_test_error\": \"CV score (RMSE)\",\n",
184175
" }\n",
185-
")\n",
186-
"fig.show(renderer=\"notebook\")"
176+
" grid_search_results[\"n_components\"] = grid_search_results[\n",
177+
" \"n_components\"\n",
178+
" ].fillna(\"None\")\n",
179+
" fig = px.scatter(\n",
180+
" grid_search_results,\n",
181+
" x=\"mean_fit_time\",\n",
182+
" y=\"mean_test_error\",\n",
183+
" error_x=\"std_fit_time\",\n",
184+
" error_y=\"std_test_score\",\n",
185+
" hover_data=grid_search_results.columns,\n",
186+
" labels=labels,\n",
187+
" )\n",
188+
" fig.update_layout(\n",
189+
" title={\n",
190+
" \"text\": \"Trade-off between fit time and mean test score\",\n",
191+
" \"y\": 0.95,\n",
192+
" \"x\": 0.5,\n",
193+
" \"xanchor\": \"center\",\n",
194+
" \"yanchor\": \"top\",\n",
195+
" }\n",
196+
" )\n",
197+
" fig.show(renderer=\"notebook\")"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": null,
203+
"metadata": {},
204+
"outputs": [],
205+
"source": [
206+
"# Write your code here."
187207
]
188208
},
189209
{
@@ -266,4 +286,4 @@
266286
},
267287
"nbformat": 4,
268288
"nbformat_minor": 5
269-
}
289+
}

0 commit comments

Comments
 (0)