Skip to content

Warn in inverse_transform when categories share an encoded value - #1121

Open
Chetansahney wants to merge 2 commits into
feature-engine:narwhals-migrationfrom
Chetansahney:fix/inverse-transform-shared-codes
Open

Chetansahney wants to merge 2 commits into
feature-engine:narwhals-migrationfrom
Chetansahney:fix/inverse-transform-shared-codes

Conversation

@Chetansahney

@Chetansahney Chetansahney commented Nov 10, 2026 •

Copy link
Copy Markdown

Encoders that use the shared inverse_transform invert encoder_dict_. When two categories were encoded to the same value, the inverted mapping keeps only one of them, so inverse_transform silently returns the same category for all of them:

import pandas as pd
from feature_engine.encoding import CountFrequencyEncoder

X = pd.DataFrame({"c": ["a", "a", "b", "b", "c", "c", "c", "c"]})
enc = CountFrequencyEncoder(encoding_method="count").fit(X)
enc.inverse_transform(enc.transform(X))["c"].tolist()
# ['b', 'b', 'b', 'b', 'c', 'c', 'c', 'c']: the "a" rows come back as "b"

This can happen with CountFrequencyEncoder (equal counts), MeanEncoder (equal target means), WoEEncoder (equal weight of evidence) and DecisionTreeEncoder (categories that end in the same leaf). OrdinalEncoder is unaffected, since its codes are unique.

As discussed, inverse_transform now raises a UserWarning naming the variable when some of its categories share an encoded value, and returns the inverse transformation as before:

UserWarning: Some categories of c were encoded to the same value. inverse_transform will return only one of those categories for all of them.

The change is 8 lines in base_encoder.py, plus one test on the mixin that checks the warning and the returned values. This PR targets narwhals-migration. The failing CI jobs fail the same way on the base branch: locally, the same 300 tests fail with and without this change, and the new test passes. flake8 is clean on the changed files.

Separately, MatchCategories exposes inverse_transform through the same mixin but has no encoder_dict_, so calling it raises AttributeError. I've left that out of this PR.

@solegalli

Copy link
Copy Markdown
Collaborator

Hi @Chetansahney thanks for the contribution and apologies for the delayed response. We are moving away from returning errors and trying to make the transformers work out of the box, raising a warning when necessary.

So for this case, it would be better to raise a warning when this happens and return the inverse transformation anyways. From the encoders you mention, I imagine that the most affected would be the count encoder, because with mean and frequency, it would be unlikely that with the commas 2 values are exactly identical. Because of this, I'd like to keep the changes to the smallest/minimum possible.

Could you please also make the PR to the narwhals-migration branch, and rebase the content from that branch into yours, because the source code has changed quite a bit respect to the main branch.

The base encoder's inverse_transform inverts encoder_dict_, so when two
categories were encoded to the same value only one of them is returned
for all of them, for example with CountFrequencyEncoder and equal
counts. Warn when this happens and return the inverse transformation as
before.
@Chetansahney
Chetansahney force-pushed the fix/inverse-transform-shared-codes branch from 9257924 to 636d821 Compare November 18, 2026 20:19
@Chetansahney
Chetansahney changed the base branch from main to narwhals-migration November 18, 2026 20:19
@Chetansahney

Copy link
Copy Markdown
Author

Thanks @solegalli, done. I rebased this onto narwhals-migration and changed the base branch of this PR to it. inverse_transform now raises a UserWarning naming the variable when some of its categories share an encoded value, and returns the inverse transformation as before. The change is 8 lines in base_encoder.py plus one test.

@Chetansahney Chetansahney changed the title Raise in inverse_transform when categories share an encoded value Warn in inverse_transform when categories share an encoded value Nov 19, 2026
…nverse-transform-shared-codes

# Conflicts:
#	feature_engine/encoding/base_encoder.py
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