Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
137 changes: 94 additions & 43 deletions python/cuml/cuml/preprocessing/_target_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cudf
import cupy as cp
import numpy as np
import pandas as pd
from sklearn.utils import ClassifierTags

from cuml.internals.base import Base
Expand Down Expand Up @@ -465,6 +466,7 @@ def _fit_transform_independent(self, df, x_cols, fold_ids):
self._encodings_per_feature = []
self.encode_all = []
result_cols = []
merged = False

for i, col in enumerate(x_cols):
out_col_i = f"out_{i}"
Expand All @@ -473,9 +475,21 @@ def _fit_transform_independent(self, df, x_cols, fold_ids):
encode_all_i = self._compute_single_feature_encoding_median(
df, col
)
# Merge encoding into df for this feature
df = df.merge(
encode_all_i.rename(columns={"out": out_col_i}),
on=col,
how="left",
)
df[out_col_i] = df[out_col_i].nans_to_nulls()
df[out_col_i] = df[out_col_i].fillna(self.y_stat_val)
merged = True
else:
encode_all_i = self._compute_single_feature_encoding(
df, col, out_col_i
encode_all_i, encoded = self._encode_single_feature(df, col)
Comment thread
maxwbuckley marked this conversation as resolved.
Resolved
# A category with no non-null targets encodes as NaN when
# smooth=0; impute it as the merge path does
df[out_col_i] = cp.where(
cp.isnan(encoded), self.y_stat_val, encoded
)

self.encode_all.append(encode_all_i)
Expand All @@ -486,42 +500,65 @@ def _fit_transform_independent(self, df, x_cols, fold_ids):
encode_all_i, col, self.categories_[i]
)
)

# Merge encoding into df for this feature
df = df.merge(
encode_all_i.rename(columns={"out": out_col_i}),
on=col,
how="left",
)
df[out_col_i] = df[out_col_i].nans_to_nulls()
df[out_col_i] = df[out_col_i].fillna(self.y_stat_val)
result_cols.append(out_col_i)

# Sort by original index and return results
df = df.sort_values("id").reset_index(drop=True)
if merged:
# Merges do not preserve row order; restore the original order
df = df.sort_values("id").reset_index(drop=True)
else:
# `df` is stored as `self.train`. A frame only modified in place
# keeps the column metadata it was constructed with, which under
# cudf.pandas holds proxied pandas objects that cannot be
# pickled; the merge path happened to rebuild it. Reassigning
# the labels normalizes the metadata (0.8 ms at 195k rows).
df.columns = list(df.columns)
res = df[result_cols].to_cupy()
return res, df

def _compute_single_feature_encoding(self, train, col, out_col):
"""Compute target encoding for a single feature column."""
# Group by single feature and compute stats
df_count = train.groupby(col, as_index=False, dropna=False).agg(
{"y": "count"}
)
df_count.columns = [col, "y_count"]
def _encode_single_feature(self, train, col):
"""Smoothed mean encoding of one feature, by factorizing it.

df_sum = train.groupby(col, as_index=False, dropna=False).agg(
{"y": "sum"}
)
df_sum.columns = [col, "y_sum"]
Returns the per-category table (``col``, ``out``), with a null row
for a missing category if one is present, and the per-row encodings
aligned with ``train``.

df = df_sum.merge(df_count, on=col, how="left")
This replaces two groupbys and two merges per column with a
factorize, two ``bincount`` calls and a gather. The cudf operations
each cost tens of milliseconds of fixed overhead regardless of row
count, which dominated ``fit_transform`` on wide inputs.
"""
codes, uniques = train[col].factorize()
codes = cp.asarray(codes)
n_cats = len(uniques)
has_null = bool((codes < 0).any())
if has_null:
codes = cp.where(codes < 0, n_cats, codes)
n_buckets = n_cats + int(has_null)

y = train["y"].to_cupy(na_value=np.nan)
if train["y"].has_nulls:
# Match the groupby semantics: null targets count for nothing
valid = ~cp.isnan(y)
counts = cp.bincount(codes[valid], minlength=n_buckets)
sums = cp.bincount(
codes[valid], weights=y[valid], minlength=n_buckets
)
else:
counts = cp.bincount(codes, minlength=n_buckets)
sums = cp.bincount(codes, weights=y, minlength=n_buckets)

# Apply smoothing
smooth = self.smooth
df["out"] = (df.y_sum + smooth * self.mean) / (df.y_count + smooth)

return df[[col, "out"]]
mean = float(self.mean)
out = (sums + smooth * mean) / (counts.astype(sums.dtype) + smooth)

keys = cudf.Series(uniques)
if has_null:
keys = cudf.concat(
[keys, cudf.Series([None], dtype=keys.dtype)],
ignore_index=True,
)
encode_all = cudf.DataFrame({col: keys, "out": out})
return encode_all, out[codes]

def _compute_single_feature_encoding_median(self, train, col):
"""Compute median target encoding for a single feature column."""
Expand All @@ -534,22 +571,36 @@ def _compute_single_feature_encoding_median(self, train, col):
def _category_encodings(self, encode_all, col, categories):
"""Encodings of `categories` in order, falling back to the mean.

Uses a merge rather than comparing values so that a missing
category matches the missing key in `encode_all`.
`encode_all` has one row per category, so this is a small table.
It is resolved on the host: a cudf merge plus sort here costs tens
of milliseconds of fixed overhead per column, which dominated
``fit_transform`` on wide inputs. A missing category (NaN/None) is
matched to the null key in `encode_all` explicitly, since NaN does
not compare equal to itself.
"""
# A missing category has to be null to match the null key in
# `encode_all`. `_cats_to_series` hardcodes `nan_as_null=True`
# (cudf's default) so the behavior doesn't switch when cudf.pandas
# is active.
cats = cudf.DataFrame(
{
col: _cats_to_series(categories),
"pos": cp.arange(len(categories)),
}
enc = encode_all[[col, "out"]].to_pandas()
keys = enc[col]
vals = enc["out"].to_numpy(dtype="float64", na_value=np.nan)
key_missing = keys.isna().to_numpy()
lut = dict(
zip(keys[~key_missing].tolist(), vals[~key_missing].tolist())
)
mean = float(self.mean)

cats = np.asarray(categories)
cat_missing = np.asarray(pd.isna(cats), dtype=bool)
out = np.array(
[
mean if missing else lut.get(cat, mean)
for cat, missing in zip(cats.tolist(), cat_missing)
],
dtype="float64",
)
cats = cats.merge(encode_all[[col, "out"]], on=col, how="left")
out = cats.sort_values("pos")["out"].fillna(float(self.mean))
return out.to_numpy()
if cat_missing.any() and key_missing.any():
out[cat_missing] = vals[key_missing][0]
# `out` of an empty group can be NaN for some statistics; the merge
# path filled those with the mean as well.
return np.where(np.isnan(out), mean, out)

def _make_fold_column(self, n_samples, fold_ids):
"""
Expand Down
17 changes: 17 additions & 0 deletions python/cuml/tests/test_target_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,20 @@ def test_as_sklearn_missing_category_matches_cuml(multi_feature_mode):
np.testing.assert_allclose(
sklearn_model.transform(X_new), encoder.transform(X_new), rtol=1e-5
)


def test_independent_fit_transform_all_null_target_category():
"""With smooth=0 a category whose targets are all null has no encoding
of its own; its training rows fall back to `y_stat_val`, not NaN."""
X = np.array(["a", "a", "c", "b", "b"], dtype=object)[:, None]
y = np.array([1.0, 1.0, 4.0, np.nan, np.nan])

encoder = TargetEncoder(
smooth=0, multi_feature_mode="independent", output_type="numpy"
)
train_encoded = encoder.fit_transform(X, y)

assert encoder.y_stat_val == 2.0
np.testing.assert_allclose(
train_encoded, np.array([1.0, 1.0, 4.0, 2.0, 2.0])[:, None]
)
Loading