Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX HistgradientBoosting with pandas extension dtypes #28385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/whats_new/v1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ Changelog
the same columns. Previously, it would raise a due to duplicated column names.
:pr:`28262` by :user:`Guillaume Lemaitre <glemaitre>`.

:mod:`sklearn.ensemble`
.......................

- |Fix| :class:`HistGradientBoostingClassifier` and
:class:`HistGradientBoostingRegressor` when fitted on `pandas` `DataFrame`
with extension dtypes, for example `pd.Int64Dtype`
:pr:`28385` by :user:`Loïc Estève <lesteve>`.

:mod:`sklearn.inspection`
.........................

Expand Down
16 changes: 9 additions & 7 deletions sklearn/ensemble/_hist_gradient_boosting/gradient_boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,15 @@ def _check_categorical_features(self, X):
Indicates whether a feature is categorical. If no feature is
categorical, this is None.
"""
if hasattr(X, "__dataframe__"):
# Special code for pandas because of a bug in recent pandas, which is
# fixed in main and maybe included in 2.2.1, see
# https://github.com/pandas-dev/pandas/pull/57173.
# Also pandas versions < 1.5.1 do not support the dataframe interchange
if _is_pandas_df(X):
X_is_dataframe = True
categorical_columns_mask = np.asarray(X.dtypes == "category")
X_has_categorical_columns = categorical_columns_mask.any()
elif hasattr(X, "__dataframe__"):
X_is_dataframe = True
categorical_columns_mask = np.asarray(
[
Expand All @@ -377,12 +385,6 @@ def _check_categorical_features(self, X):
]
)
X_has_categorical_columns = categorical_columns_mask.any()
# pandas versions < 1.5.1 do not support the dataframe interchange
# protocol so we inspect X.dtypes directly
elif _is_pandas_df(X):
X_is_dataframe = True
categorical_columns_mask = np.asarray(X.dtypes == "category")
X_has_categorical_columns = categorical_columns_mask.any()
else:
X_is_dataframe = False
categorical_columns_mask = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1669,3 +1669,15 @@ def joblib_dump_with_different_bitness():
new_clf = joblib.load(joblib_dump_with_different_bitness())
new_score = new_clf.score(X, y)
assert score == pytest.approx(new_score)


def test_pandas_nullable_dtype():
# Non regression test for https://github.com/scikit-learn/scikit-learn/issues/28317
pd = pytest.importorskip("pandas")

rng = np.random.default_rng(0)
X = pd.DataFrame({"a": rng.integers(10, size=100)}).astype(pd.Int64Dtype())
y = rng.integers(2, size=100)

clf = HistGradientBoostingClassifier()
clf.fit(X, y)