|
63 | 63 | from sklearn.metrics import roc_auc_score |
64 | 64 | from sklearn.impute import SimpleImputer |
65 | 65 | from sklearn.pipeline import Pipeline |
66 | | -from sklearn.linear_model import Ridge, SGDClassifier |
| 66 | +from sklearn.linear_model import Ridge, SGDClassifier, LinearRegression |
67 | 67 |
|
68 | 68 | from sklearn.model_selection.tests.common import OneTimeSplitter |
69 | 69 |
|
@@ -198,6 +198,24 @@ def test_grid_search(): |
198 | 198 | assert_raises(ValueError, grid_search.fit, X, y) |
199 | 199 |
|
200 | 200 |
|
| 201 | +def test_grid_search_pipeline_steps(): |
| 202 | + # check that parameters that are estimators are cloned before fitting |
| 203 | + pipe = Pipeline([('regressor', LinearRegression())]) |
| 204 | + param_grid = {'regressor': [LinearRegression(), Ridge()]} |
| 205 | + grid_search = GridSearchCV(pipe, param_grid, cv=2) |
| 206 | + grid_search.fit(X, y) |
| 207 | + regressor_results = grid_search.cv_results_['param_regressor'] |
| 208 | + assert isinstance(regressor_results[0], LinearRegression) |
| 209 | + assert isinstance(regressor_results[1], Ridge) |
| 210 | + assert not hasattr(regressor_results[0], 'coef_') |
| 211 | + assert not hasattr(regressor_results[1], 'coef_') |
| 212 | + assert regressor_results[0] is not grid_search.best_estimator_ |
| 213 | + assert regressor_results[1] is not grid_search.best_estimator_ |
| 214 | + # check that we didn't modify the parameter grid that was passed |
| 215 | + assert not hasattr(param_grid['regressor'][0], 'coef_') |
| 216 | + assert not hasattr(param_grid['regressor'][1], 'coef_') |
| 217 | + |
| 218 | + |
201 | 219 | def check_hyperparameter_searcher_with_fit_params(klass, **klass_kwargs): |
202 | 220 | X = np.arange(100).reshape(10, 10) |
203 | 221 | y = np.array([0] * 5 + [1] * 5) |
|
0 commit comments