Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions python/cuml/cuml/preprocessing/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,18 @@ def _compute_drop_idx(self):
).format(feature)
raise ValueError(msg)
cats = self._encoders[feature].classes_
if not self.drop[feature].isin(cats).all():
drop_vals = self.drop[feature]
if drop_vals.dtype != cats.dtype:
drop_vals = drop_vals.astype(cats.dtype)
Comment thread
betatim marked this conversation as resolved.
Resolved
if not drop_vals.isin(cats).all():
msg = (
"Some categories for feature {} were supposed "
"to be dropped, but were not found in the encoder "
"categories.".format(feature)
)
raise ValueError(msg)
cats = cudf.Series(cats)
idx = cats.isin(self.drop[feature])
idx = cats.isin(drop_vals)
drop_idx[feature] = cp.asarray(cats[idx].index)
Comment thread
betatim marked this conversation as resolved.
Resolved
return drop_idx
else:
Expand All @@ -317,6 +320,8 @@ def _check_input_fit(self, X, is_categories=False):

def _has_unknown(self, X_cat, encoder_cat):
"""Check if X_cat has categories that are not present in encoder_cat."""
if hasattr(encoder_cat, "dtype") and X_cat.dtype != encoder_cat.dtype:
Comment thread
betatim marked this conversation as resolved.
Resolved
encoder_cat = encoder_cat.astype(X_cat.dtype)
return not X_cat.isin(encoder_cat).all()

@generate_docstring(y=None)
Expand Down
4 changes: 3 additions & 1 deletion python/cuml/cuml/preprocessing/onehotencoder_mg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
# SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION.
# SPDX-License-Identifier: Apache-2.0
#

Expand Down Expand Up @@ -45,4 +45,6 @@ def _unique(self, inp):
return inp.unique().compute()

def _has_unknown(self, X_cat, encoder_cat):
if hasattr(encoder_cat, "dtype") and X_cat.dtype != encoder_cat.dtype:
encoder_cat = encoder_cat.astype(X_cat.dtype)
return not X_cat.isin(encoder_cat).all().compute()
46 changes: 11 additions & 35 deletions python/cuml/tests/dask/test_dask_one_hot_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ def test_onehot_vs_skonehot(client):


@pytest.mark.mg
@pytest.mark.xfail(
reason="Dask OneHotEncoder dtype/string issues; see https://github.com/rapidsai/cuml/issues/7826",
strict=True,
)
@pytest.mark.parametrize(
"drop", [None, "first", {"g": Series("F"), "i": Series(3)}]
)
Expand All @@ -50,15 +46,12 @@ def test_onehot_inverse_transform(client, drop):
ohe = enc.fit_transform(X)
inv = enc.inverse_transform(ohe)
assert_frame_equal(
inv.compute().to_pandas().reset_index(drop=True), df.to_pandas()
inv.compute().to_pandas().reset_index(drop=True),
X.compute().to_pandas().reset_index(drop=True),

@betatim betatim Feb 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X is what is passed to fit_transform so it seems reasonable to use it as reference for the result of inverse_transform. And round tripping it through dask-cudf means we should get what ever behaviour it has (across versions)?!

)


@pytest.mark.mg
@pytest.mark.xfail(
reason="Dask OneHotEncoder dtype/string issues; see https://github.com/rapidsai/cuml/issues/7826",
strict=True,
)
def test_onehot_categories(client):
X = DataFrame({"chars": ["a", "b"], "int": [0, 2]})
X = dask_cudf.from_cudf(X, npartitions=2)
Expand Down Expand Up @@ -105,10 +98,6 @@ def test_onehot_transform_handle_unknown(client):


@pytest.mark.mg
@pytest.mark.xfail(
reason="Dask OneHotEncoder dtype/string issues; see https://github.com/rapidsai/cuml/issues/7826",
strict=True,
)
def test_onehot_inverse_transform_handle_unknown(client):
X = DataFrame({"chars": ["a", "b"], "int": [0, 2]})
X = dask_cudf.from_cudf(X, npartitions=2)
Expand All @@ -119,7 +108,8 @@ def test_onehot_inverse_transform_handle_unknown(client):
enc = enc.fit(X)
df = enc.inverse_transform(Y_ohe)
ref = DataFrame({"chars": [None, "b"], "int": [0, 2]})
assert_frame_equal(df.compute().to_pandas(), ref.to_pandas())
ref = dask_cudf.from_cudf(ref, npartitions=1).compute().to_pandas()
assert_frame_equal(df.compute().to_pandas(), ref)


@pytest.mark.mg
Expand Down Expand Up @@ -152,10 +142,6 @@ def test_onehot_random_inputs(client, drop, as_array, sparse, n_samples):


@pytest.mark.mg
@pytest.mark.xfail(
reason="Dask OneHotEncoder dtype/string issues; see https://github.com/rapidsai/cuml/issues/7826",
strict=True,
)
def test_onehot_drop_idx_first(client):
X_ary = [["c", 2, "a"], ["b", 2, "b"]]
X = DataFrame({"chars": ["c", "b"], "int": [2, 2], "letters": ["a", "b"]})
Expand All @@ -168,15 +154,12 @@ def test_onehot_drop_idx_first(client):
cp.testing.assert_array_equal(ohe.compute(), ref)
inv = enc.inverse_transform(ohe)
assert_frame_equal(
inv.compute().to_pandas().reset_index(drop=True), X.to_pandas()
inv.compute().to_pandas().reset_index(drop=True),
ddf.compute().to_pandas().reset_index(drop=True),
)


@pytest.mark.mg
@pytest.mark.xfail(
reason="Dask OneHotEncoder dtype/string issues; see https://github.com/rapidsai/cuml/issues/7826",
strict=True,
)
def test_onehot_drop_one_of_each(client):
X_ary = [["c", 2, "a"], ["b", 2, "b"]]
X = DataFrame({"chars": ["c", "b"], "int": [2, 2], "letters": ["a", "b"]})
Expand All @@ -190,7 +173,8 @@ def test_onehot_drop_one_of_each(client):
cp.testing.assert_array_equal(ohe.compute(), ref)
inv = enc.inverse_transform(ohe)
assert_frame_equal(
inv.compute().to_pandas().reset_index(drop=True), X.to_pandas()
inv.compute().to_pandas().reset_index(drop=True),
ddf.compute().to_pandas().reset_index(drop=True),
)


Expand All @@ -199,14 +183,10 @@ def test_onehot_drop_one_of_each(client):
"drop, pattern",
[
[dict({"chars": "b"}), "`drop` should have as many columns"],
pytest.param(
[
dict({"chars": "b", "int": [2, 0]}),
"Trying to drop multiple values",
marks=pytest.mark.xfail(
reason="Dask OneHotEncoder dtype/string issues; see https://github.com/rapidsai/cuml/issues/7826",
strict=True,
),
),
],
[
dict({"chars": "b", "int": 3}),
"Some categories [a-zA-Z, ]* were not found",
Expand All @@ -226,10 +206,6 @@ def test_onehot_drop_exceptions(client, drop, pattern):


@pytest.mark.mg
@pytest.mark.xfail(
reason="Dask OneHotEncoder dtype/string issues; see https://github.com/rapidsai/cuml/issues/7826",
strict=True,
)
def test_onehot_get_categories(client):
X = DataFrame({"chars": ["c", "b", "d"], "ints": [2, 1, 0]})
X = dask_cudf.from_cudf(X, npartitions=2)
Expand All @@ -239,4 +215,4 @@ def test_onehot_get_categories(client):
cats = enc.categories_

for i in range(len(ref)):
np.testing.assert_array_equal(ref[i], cats[i].to_numpy())
np.testing.assert_array_equal(ref[i], cats[i].to_pandas().to_numpy())