Skip to content
Open
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
Next Next commit
TargetEncoder: factorize + bincount per feature instead of groupby/merge
The per-feature fit path paid two groupbys, two merges and a sort per
column, each with tens of milliseconds of fixed cudf overhead, plus a
merge and sort per column to order the encodings by categories_.
Factorize the column, bincount sums and counts, gather the per-row
encoding, and resolve the per-category table on the host. Outputs are
unchanged. 195k x 32 fit_transform: 2.5 s -> 0.8 s; 250k x 128: 10.5 s
-> 3.4 s.

Part of #8807.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
  • Loading branch information
maxwbuckley and claude committed Oct 22, 2026
commit c22b91657a009d29bc1caa20959dfbb84ab6864f
128 changes: 84 additions & 44 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,10 +475,18 @@ def _fit_transform_independent(self, df, x_cols, fold_ids):
encode_all_i = self._compute_single_feature_encoding_median(
df, col
)
else:
encode_all_i = self._compute_single_feature_encoding(
df, col, out_col_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)
merged = True
else:
encode_all_i, encoded = self._encode_single_feature(df, col)
Comment thread
maxwbuckley marked this conversation as resolved.
Resolved
df[out_col_i] = encoded

self.encode_all.append(encode_all_i)

Expand All @@ -486,42 +496,58 @@ 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)
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 +560,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
Loading