|
22 | 22 | make_multilabel_classification,
|
23 | 23 | )
|
24 | 24 | from sklearn.ensemble import HistGradientBoostingClassifier
|
| 25 | +from sklearn.experimental import enable_halving_search_cv # noqa |
25 | 26 | from sklearn.impute import SimpleImputer
|
26 | 27 | from sklearn.linear_model import LinearRegression, Ridge, SGDClassifier
|
27 | 28 | from sklearn.metrics import (
|
|
38 | 39 | GridSearchCV,
|
39 | 40 | GroupKFold,
|
40 | 41 | GroupShuffleSplit,
|
| 42 | + HalvingGridSearchCV, |
41 | 43 | KFold,
|
42 | 44 | LeaveOneGroupOut,
|
43 | 45 | LeavePGroupsOut,
|
@@ -2420,3 +2422,31 @@ def test_search_cv_verbose_3(capsys, return_train_score):
|
2420 | 2422 | else:
|
2421 | 2423 | match = re.findall(r"score=[\d\.]+", captured)
|
2422 | 2424 | assert len(match) == 3
|
| 2425 | + |
| 2426 | + |
| 2427 | +@pytest.mark.parametrize( |
| 2428 | + "SearchCV, param_search", |
| 2429 | + [ |
| 2430 | + (GridSearchCV, "param_grid"), |
| 2431 | + (RandomizedSearchCV, "param_distributions"), |
| 2432 | + (HalvingGridSearchCV, "param_grid"), |
| 2433 | + ], |
| 2434 | +) |
| 2435 | +def test_search_estimator_param(SearchCV, param_search): |
| 2436 | + # test that SearchCV object doesn't change the object given in the parameter grid |
| 2437 | + X, y = make_classification(random_state=42) |
| 2438 | + |
| 2439 | + params = {"clf": [LinearSVC(dual="auto")], "clf__C": [0.01]} |
| 2440 | + orig_C = params["clf"][0].C |
| 2441 | + |
| 2442 | + pipe = Pipeline([("trs", MinimalTransformer()), ("clf", None)]) |
| 2443 | + |
| 2444 | + param_grid_search = {param_search: params} |
| 2445 | + gs = SearchCV(pipe, refit=True, cv=2, scoring="accuracy", **param_grid_search).fit( |
| 2446 | + X, y |
| 2447 | + ) |
| 2448 | + |
| 2449 | + # testing that the original object in params is not changed |
| 2450 | + assert params["clf"][0].C == orig_C |
| 2451 | + # testing that the GS is setting the parameter of the step correctly |
| 2452 | + assert gs.best_estimator_.named_steps["clf"].C == 0.01 |
0 commit comments