|
17 | 17 | from sklearn import config_context
|
18 | 18 | from sklearn.base import BaseEstimator, ClassifierMixin, is_classifier
|
19 | 19 | from sklearn.cluster import KMeans
|
| 20 | +from sklearn.compose import ColumnTransformer |
20 | 21 | from sklearn.datasets import (
|
21 | 22 | make_blobs,
|
22 | 23 | make_classification,
|
|
64 | 65 | from sklearn.naive_bayes import ComplementNB
|
65 | 66 | from sklearn.neighbors import KernelDensity, KNeighborsClassifier, LocalOutlierFactor
|
66 | 67 | from sklearn.pipeline import Pipeline
|
67 |
| -from sklearn.preprocessing import StandardScaler |
| 68 | +from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder, StandardScaler |
68 | 69 | from sklearn.svm import SVC, LinearSVC
|
69 | 70 | from sklearn.tests.metadata_routing_common import (
|
70 | 71 | ConsumingScorer,
|
@@ -1403,9 +1404,7 @@ def test_search_cv_results_none_param():
|
1403 | 1404 | est_parameters,
|
1404 | 1405 | cv=cv,
|
1405 | 1406 | ).fit(X, y)
|
1406 |
| - assert_array_equal( |
1407 |
| - grid_search.cv_results_["param_random_state"], [0, float("nan")] |
1408 |
| - ) |
| 1407 | + assert_array_equal(grid_search.cv_results_["param_random_state"], [0, None]) |
1409 | 1408 |
|
1410 | 1409 |
|
1411 | 1410 | @ignore_warnings()
|
@@ -2686,3 +2685,36 @@ def score(self, X, y):
|
2686 | 2685 | grid_search.fit(X, y)
|
2687 | 2686 | for param in param_grid:
|
2688 | 2687 | assert grid_search.cv_results_[f"param_{param}"].dtype == object
|
| 2688 | + |
| 2689 | + |
| 2690 | +def test_search_with_estimators_issue_29157(): |
| 2691 | + """Check cv_results_ for estimators with a `dtype` parameter, e.g. OneHotEncoder.""" |
| 2692 | + pd = pytest.importorskip("pandas") |
| 2693 | + df = pd.DataFrame( |
| 2694 | + { |
| 2695 | + "numeric_1": [1, 2, 3, 4, 5], |
| 2696 | + "object_1": ["a", "a", "a", "a", "a"], |
| 2697 | + "target": [1.0, 4.1, 2.0, 3.0, 1.0], |
| 2698 | + } |
| 2699 | + ) |
| 2700 | + X = df.drop("target", axis=1) |
| 2701 | + y = df["target"] |
| 2702 | + enc = ColumnTransformer( |
| 2703 | + [("enc", OneHotEncoder(sparse_output=False), ["object_1"])], |
| 2704 | + remainder="passthrough", |
| 2705 | + ) |
| 2706 | + pipe = Pipeline( |
| 2707 | + [ |
| 2708 | + ("enc", enc), |
| 2709 | + ("regressor", LinearRegression()), |
| 2710 | + ] |
| 2711 | + ) |
| 2712 | + grid_params = { |
| 2713 | + "enc__enc": [ |
| 2714 | + OneHotEncoder(sparse_output=False), |
| 2715 | + OrdinalEncoder(), |
| 2716 | + ] |
| 2717 | + } |
| 2718 | + grid_search = GridSearchCV(pipe, grid_params, cv=2) |
| 2719 | + grid_search.fit(X, y) |
| 2720 | + assert grid_search.cv_results_["param_enc__enc"].dtype == object |
0 commit comments