Repository navigation
Description
Describe the workflow you want to enable
Currently the drop parameter in OneHotEncoder objects support {‘first’, ‘if_binary’, <array-of-features>, None} as potential choices, with a strong encouragement to use None to allow proper use of regularized linear modeling downstream. However, if there exists features that are constant for a data matrix then it would be really great if OneHotEncoder took care of dropping that feature entirely during coding. If all columns in the input matrix were constant perhaps an error could be raised during .fit(...) when this option was used for object initialization.
I would like to do the following.
>>> oh_encoder = OneHotEncoder(
sparse_output=False,
drop='constant',
handle_unknown='ignore')
So that this were possible.
>>> X
array([['M', 'O', 'M'],
['M', 'A', 'N']], dtype='<U1')
>>> oh_encoder.fit_transform(X)
array([[0., 1., 0., 1.],
[1., 0., 1., 0.]], dtype='float')
>>> oh_encoder.inverse_transform(oh_encoder.transform(X))
array([['M', 'O', 'M'],
['M', 'A', 'N']], dtype='<U1')
Please note that this is distinct from using drop='first' which would definitely remove the constant columns but also the first column for each feature group in the data matrix columns, which would be pretty crazy. Perhaps, instead of None this could become the default choice as well, as this encoding perfectly preserves all the information in the input data matrix.
Describe your proposed solution
It would be great to have drop='constant' as an option.
Describe alternatives you've considered, if relevant
I have considered writing my own OneHotEncoder, but it feels redudant and error prone to do so. I have also tried to specify the constant features per column via the drop=<array> method, but there are columns for which I set values to None (corresponding to the non-constant columns) but the object didn't like that.
Additional context
None.
Activity
glemaitre commentedon Sep 22, 2023
I feel that our
OneHotEncoderalready has a lot of parameters. Since the feature being constant is not specific to theOneHotEncoder, I am thinking that we should not have the option into it. Indeed, I would prefer to have a specific transformer that only detect constant column seen atfitand drop them attransform. In this way, you could use this transformer ahead of several other transformation.Somehow it could also be useful in the context discussed in: #19547
ayaanhossain commentedon Sep 22, 2023
Is there such a transformer available in the library that removes the constant columns and adds them back at the
inverse_transform(...)time?ayaanhossain commentedon Sep 22, 2023
Part of the reason it would be great to have this within the
OneHotEncoderobject itself is that it automatically would take care of the removed offsets when going forget_feature_names_outfor example.glemaitre commentedon Sep 23, 2023
Having a pipeline with such a transformer will also propagate the feature names in this manner.
ayaanhossain commentedon Oct 11, 2023
I ended up sub classing
OneHotEncoderwhich gets the job done as I intended. Code below.Since, I've solved the problem, I am open to closing this issue. However, I still do think a similar functionality within either the
OneHotEncodernatively or with a separate preprocessor with appropriate support for inverse transformation would be appreciated.glemaitre commentedon Nov 4, 2023
I did not think of it at first but you can use the
VarianceThresholdfor this purpose.