Repository navigation
Warn in inverse_transform when categories share an encoded value - #1121
Chetansahney wants to merge 2 commits into
Conversation
|
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.
9257924 to
636d821
Compare
|
Thanks @solegalli, done. I rebased this onto |
…nverse-transform-shared-codes # Conflicts: # feature_engine/encoding/base_encoder.py
Encoders that use the shared
inverse_transforminvertencoder_dict_. When two categories were encoded to the same value, the inverted mapping keeps only one of them, soinverse_transformsilently returns the same category for all of them:This can happen with
CountFrequencyEncoder(equal counts),MeanEncoder(equal target means),WoEEncoder(equal weight of evidence) andDecisionTreeEncoder(categories that end in the same leaf).OrdinalEncoderis unaffected, since its codes are unique.As discussed,
inverse_transformnow raises aUserWarningnaming 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 on the mixin that checks the warning and the returned values. This PR targetsnarwhals-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,
MatchCategoriesexposesinverse_transformthrough the same mixin but has noencoder_dict_, so calling it raisesAttributeError. I've left that out of this PR.