Skip to content

Keep the fitted column count in OneHotEncoder.transform - #1093

Open
vishalhabib99 wants to merge 1 commit into
uber:masterfrom
vishalhabib99:fix-onehot-transform-column-count
Open

vishalhabib99 wants to merge 1 commit into
uber:masterfrom
vishalhabib99:fix-onehot-transform-column-count

Conversation

@vishalhabib99

Copy link
Copy Markdown

Proposed changes

OneHotEncoder._transform_col returned None when a column had no non-zero label in the data being transformed, and transform then skipped that column. That is fine at fit time, where every column with dummies has at least one non-zero label, but on new data a column can hold only its base (or infrequent) values. The column then contributes nothing, the output has fewer columns than the fitted encoder produces, and every later column's dummies shift left into the wrong positions.

Reproduction on current master (089a882):

import pandas as pd
from causalml.features import OneHotEncoder

train = pd.DataFrame({"a": ["x"] * 10 + ["y"] * 10, "b": ["u"] * 10 + ["v"] * 10})
test = pd.DataFrame({"a": ["x"] * 5, "b": ["u"] * 2 + ["v"] * 3})
ohe = OneHotEncoder(min_obs=2).fit(train)
print(ohe.transform(train).shape, ohe.transform(test).shape)
print(ohe.transform(test).toarray())

Before:

(20, 2) (5, 1)
[[0]
 [0]
 [1]
 [1]
 [1]]

Column b's dummy lands in the slot that belongs to a.

After:

(20, 2) (5, 2)
[[0 0]
 [0 0]
 [0 1]
 [0 1]
 [0 1]]

The fix returns an all-zero sparse block of the fitted width (label_max) for such a column. None is still returned when the column has no dummy at all (label_max of 0, or NaN when no value reaches min_obs), so fit_transform output and load_data behavior are unchanged.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Further comments

Added test_OneHotEncoder_transform_keeps_fitted_columns to tests/test_features.py. It fails on master (assert (5, 1) == (5, 2)) and passes with the fix. pytest tests/test_features.py tests/test_match.py (match uses load_data): 18 passed. black --check and flake8 are clean on the changed files.

OneHotEncoder._transform_col returned None for a column whose rows in the
given data held no non-zero label, so transform skipped that column. On new
data where a column only takes its base or infrequent values, the output had
fewer columns than at fit time and every later column's dummies shifted
left. Return an all-zero block of the fitted width instead, and keep None
only for columns that have no dummy at all (label_max of 0 or NaN).
@cla-assistant

cla-assistant Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cla-assistant

cla-assistant Bot commented Nov 30, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fitted OneHotEncoder(min_obs=2) on three columns and transformed new data where one column holds only its base value. Master returned 2 columns instead of the fitted 4, so the other dummies shifted left; here it's 4, and each row matches its encoding when transformed together with the training data. Unseen or infrequent values and normal rows give the same output as before.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants