[go: up one dir, main page]

Skip to content
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

FIX Target encoder const y #28233

Merged
merged 10 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -105,6 +105,14 @@ Changelog
and `axis=1`, as documented in the docstring.
:pr:`28222` by :user:`Guillaume Lemaitre <glemaitre>`.


:mod:`sklearn.preprocessing`
............................

- |Fix| :class:`preprocessing.TargetEncoder` no longer fails when `target_type="continuous"` and the input is read-only.
In particular, it now works with pandas copy-on-write mode enabled.
:pr:`28233` by :user:`John Hopfensperger <s-banach>`.
s-banach marked this conversation as resolved.
Show resolved Hide resolved

.. _changes_1_4:

Version 1.4.0
Expand Down
4 changes: 2 additions & 2 deletions sklearn/preprocessing/_target_encoder_fast.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ctypedef fused Y_DTYPE:

def _fit_encoding_fast(
INT_DTYPE[:, ::1] X_int,
Y_DTYPE[:] y,
const Y_DTYPE[:] y,
cnp.int64_t[::1] n_categories,
double smooth,
double y_mean,
Expand Down Expand Up @@ -79,7 +79,7 @@ def _fit_encoding_fast(

def _fit_encoding_fast_auto_smooth(
INT_DTYPE[:, ::1] X_int,
Y_DTYPE[:] y,
const Y_DTYPE[:] y,
cnp.int64_t[::1] n_categories,
double y_mean,
double y_variance,
Expand Down
13 changes: 13 additions & 0 deletions sklearn/preprocessing/tests/test_target_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,3 +701,16 @@ def test_target_encoding_for_linear_regression(smooth, global_random_seed):
# cardinality yet non-informative feature instead of the lower
# cardinality yet informative feature:
assert abs(coef[0]) < abs(coef[2])


def test_pandas_copy_on_write():
"""
Test target-encoder cython code when y is read-only.

The numpy array underlying df["y"] is read-only when copy-on-write is enabled.
Non-regression test for gh-27879.
"""
pd = pytest.importorskip("pandas", minversion="2.0")
with pd.option_context("mode.copy_on_write", True):
df = pd.DataFrame({"x": ["a", "b", "b"], "y": [4.0, 5.0, 6.0]})
TargetEncoder(target_type="continuous").fit(df[["x"]], df["y"])
Loading