|
156 | 156 | # Here, let us see how we can use the tracking of items with this function. |
157 | 157 |
|
158 | 158 | # %% |
159 | | -# We run several cross-validations using several values of a hyperparameter: |
| 159 | +# Let us load some data: |
160 | 160 |
|
161 | 161 | # %% |
162 | 162 | from sklearn import datasets |
163 | | -from sklearn.linear_model import Lasso |
| 163 | + |
| 164 | +X, y = datasets.load_diabetes(return_X_y=True) |
| 165 | +X, y = X[:150], y[:150] |
| 166 | + |
| 167 | +# %% |
| 168 | +# Suppose that some users are coding in their draft notebook and are iterating on some |
| 169 | +# cross-validations in separate cells and forgot to store the intermediate results: |
| 170 | + |
| 171 | +# %% |
164 | 172 | import skore |
| 173 | +from sklearn.linear_model import Lasso |
165 | 174 |
|
166 | | -diabetes = datasets.load_diabetes() |
167 | | -X = diabetes.data[:150] |
168 | | -y = diabetes.target[:150] |
169 | | -lasso = Lasso() |
| 175 | +skore.cross_validate(Lasso(alpha=0.5), X, y, cv=5, project=my_project) |
170 | 176 |
|
171 | | -for alpha in [0.5, 1, 2]: |
172 | | - cv_results = skore.cross_validate( |
173 | | - Lasso(alpha=alpha), X, y, cv=5, project=my_project |
174 | | - ) |
| 177 | +# %% |
| 178 | +skore.cross_validate(Lasso(alpha=1), X, y, cv=5, project=my_project) |
| 179 | + |
| 180 | +# %% |
| 181 | +skore.cross_validate(Lasso(alpha=2), X, y, cv=5, project=my_project) |
175 | 182 |
|
176 | 183 | # %% |
177 | | -# We can compare the metrics of each run of the cross-validation (on all splits): |
| 184 | +# Thanks to the storage of the results by :func:`skore.cross_validate` (done when |
| 185 | +# specifying the ``project`` argument), we can compare the metrics of each |
| 186 | +# cross-validation run (on all splits): |
178 | 187 |
|
179 | 188 | # %% |
180 | 189 | fig_plotly = my_project.get_item("cross_validation_aggregated").plot |
|
183 | 192 | # %% |
184 | 193 | # Hence, we can observe that the first run, with ``alpha=0.5``, works better. |
185 | 194 |
|
| 195 | +# %% |
| 196 | +# .. note:: |
| 197 | +# The good practice, instead of running several cross-validations with different |
| 198 | +# values of the hyperparameter, would have been to use a |
| 199 | +# :func:`sklearn.model_selection.GridSearchCV`. |
| 200 | + |
186 | 201 | # %% |
187 | 202 | # Cleanup the project |
188 | 203 | # ------------------- |
|
0 commit comments