You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Imputer(with add_indicator_features=True) can create arrays of different shape in transform() than in fit_transform() #6767
Hi, thanks for the report. In the documentation for the imputer, it is mentioned
If True, the transformed X will have binary indicator features appended. These correspond to input features with at least one missing value marking which elements have been imputed.
So in the present use case, in the first case there is one feature with a missing value, hence an additional column is appended. And in the second case, since there is no missing value in any feature, no new column is appended.
Hope it helps and please do share if you have any inputs to explicitly mention this behaviour. Thanks.
Sorry, the actual problem is not clearly visible from the way I posted the issue.
The python code I posted aren't two use cases, they are the same use case, namely fitting a machine learning pipeline and using that later on to predict for unseen values. What's happening is that after calling fit_transform(), I have more features than after calling fit(), something which breaks a classifier used afterwards.
Actually, you are doing the very same in the examples you have checked in:
Sorry if I am understanding it wrong here. But the fit_transform is called on X_train while the transform is called on X_test, at least in the above mentioned snippet. Is there a case where fit_transform and transform are giving different shapes for the same input ? If so, that is definitely wrong and can you kindly give a reproducible example demonstrating that behaviour ? Thanks.
And sorry if I treated them as separate use cases. I understand that fit and transform are part of the pipeline and the example was on that lines. But the part I was referring to is that the input being transformed are different in the two imputations being performed. Hope I got it right now.
No, there is no case where it happens that the same input leads to different output shapes. After looking at the code, and the use case I see for the Imputer, what I'm trying to say is that the array imputed_features_ should be set in the fit method and then be re-used in the transform and fit_transform method. This ensures that each call to transform, for different inputs X, results in the same amount of features. What the code right now is doing is learning which value to use for imputation, but adding the indicator features only if an imputation takes place. In order to use it in a pipeline, I would have to write custom code to ensure the output array always has the same length.
You can also reproduce the problem I have with the example missing_values.py by changing the missing rate to 0.01.
To help me understand your point of view, could you please give me an example in which it is useful to have a different amount of features in transform depending on the given input?
changed the title [-]Imputer(with add_indicator_features=True) can create arrays of different shape in transform()[/-][+]Imputer(with add_indicator_features=True) can create arrays of different shape in transform() than in fit_transform()[/+]on May 6, 2016
Thank you so much for the clarification. I understand that custom code needs to be written since the output shape depends basically on the array being transformed rather than the one being used for fit. As you rightly pointed out, the array used for fit is just used to calculate the statistics and the array used for transform decides the shape of the output since it is the one being imputed. This was the approach taken in the implementation. Apologies for my ignorance but I don't have a convincing example to put across my point since it was suggested as an improvement by the core devs.
Indeed, the output shape needs to depend on the array it was fit on, not the array being transformed. Thank you for the bug report @mfeurer, and sorry we somehow missed this in the implementation of add_indicator_features. And apologies to @maniteja123 if you were led to believe this was correct operation.
Hi, thanks @jnothman for clarifying. It is me who needs to apologise for misinterpreting the function of imputed feature indicator. Thanks @mfeurer for detailed explanation for the correct implementation. Please feel free to fix the issue but I would be glad to get it done if it fine by you. Thanks.
Hi, sorry for the delay. I would be glad to complete the work on it. The tests are failing for the sparse case in some previous versions. I would be thankful if someone has more insight into the reason. I will try to fixing the errors. Besides the error, it would be helpful if the functionality is the expected one now. Thanks!
A design decision. When a feature has missing values in transform but not in fit should we:
have indicator features only for those features with missing values in training
have an indicator for all features
allow the user to specify the set of features for an indicator and otherwise default to 1 or 2
have a further "any feature has missing" indicator feature to handle this case
In a traditional fit/predict pipeline, some indicator features created by {2,3} may be constantly 0 at fit time, and so have no impact on the fitted model. However, these could benefit in a partial_fit approach... Option 4 actually allows the model to exploit knowledge of data absence in general, and indeed could be offered alongside any of the prior options.
I agree on your point why Option 1 is a bad idea and I think Option 4 is some kind of 'meta'-information which shouldn't be added by the imputer. My favourite is Option 3, with a fallback to Option 2.
I don't particularly like option 2, because certainly in the dense data case, you can waste a lot of space when only a few features are liable to be missing.
I actually do quite like option 4 -- I think its benefits from a learning perspective are most clear -- but maybe it needs another parameter, add_global_indicator_feature (yuck name!) to switch on.
Practically, I'd be happy to have option 1 or 2 implemented now by default in #6782, and then 3 and 4 can come through additional parameters later.
I don't know which test failures you're referring to.
Option 4 is some kind of 'meta'-information which shouldn't be added by the imputer.
I agree, option 4 is basically a compounded feature. Not sure if we want to do that at imputer?
Option 1 feels implicit...
Option 3 (provisioned via a list attr missing_features) with a fallback to Option 2 (when missing_features is None) looks good to me. It is more explicit.
When the feature is a constant 0, most estimators ignore it. Maybe if you want it removed, we could add a RemoveConstantFeature transformer?
Option 4 is a separate feature request. Maybe should be raised as a separate issue?
How about failing on transform if indicator features are enabled and new features have missing values and no set was specified?
With no other option implemented to suppress that error, I think simply raising an error would give them no control other than to add dummy rows (to their fit data) with missing values in features that may go missing at transform.
Activity
maniteja123 commentedon May 6, 2016
Hi, thanks for the report. In the documentation for the imputer, it is mentioned
So in the present use case, in the first case there is one feature with a missing value, hence an additional column is appended. And in the second case, since there is no missing value in any feature, no new column is appended.
Hope it helps and please do share if you have any inputs to explicitly mention this behaviour. Thanks.
mfeurer commentedon May 6, 2016
Sorry, the actual problem is not clearly visible from the way I posted the issue.
The python code I posted aren't two use cases, they are the same use case, namely fitting a machine learning pipeline and using that later on to predict for unseen values. What's happening is that after calling fit_transform(), I have more features than after calling fit(), something which breaks a classifier used afterwards.
Actually, you are doing the very same in the examples you have checked in:
scikit-learn/examples/missing_values.py
Line 81 in 18396be
maniteja123 commentedon May 6, 2016
Sorry if I am understanding it wrong here. But the fit_transform is called on X_train while the transform is called on X_test, at least in the above mentioned snippet. Is there a case where fit_transform and transform are giving different shapes for the same input ? If so, that is definitely wrong and can you kindly give a reproducible example demonstrating that behaviour ? Thanks.
maniteja123 commentedon May 6, 2016
And sorry if I treated them as separate use cases. I understand that fit and transform are part of the pipeline and the example was on that lines. But the part I was referring to is that the input being transformed are different in the two imputations being performed. Hope I got it right now.
mfeurer commentedon May 6, 2016
No, there is no case where it happens that the same input leads to different output shapes. After looking at the code, and the use case I see for the Imputer, what I'm trying to say is that the array
imputed_features_should be set in thefitmethod and then be re-used in thetransformandfit_transformmethod. This ensures that each call totransform, for different inputs X, results in the same amount of features. What the code right now is doing is learning which value to use for imputation, but adding the indicator features only if an imputation takes place. In order to use it in a pipeline, I would have to write custom code to ensure the output array always has the same length.You can also reproduce the problem I have with the example
missing_values.pyby changing the missing rate to0.01.To help me understand your point of view, could you please give me an example in which it is useful to have a different amount of features in
transformdepending on the given input?[-]Imputer(with add_indicator_features=True) can create arrays of different shape in transform()[/-][+]Imputer(with add_indicator_features=True) can create arrays of different shape in transform() than in fit_transform()[/+]mfeurer commentedon May 6, 2016
EDIT: I updated the title to make the issue more descriptive.
maniteja123 commentedon May 6, 2016
Thank you so much for the clarification. I understand that custom code needs to be written since the output shape depends basically on the array being transformed rather than the one being used for fit. As you rightly pointed out, the array used for fit is just used to calculate the statistics and the array used for transform decides the shape of the output since it is the one being imputed. This was the approach taken in the implementation. Apologies for my ignorance but I don't have a convincing example to put across my point since it was suggested as an improvement by the core devs.
jnothman commentedon May 7, 2016
Indeed, the output shape needs to depend on the array it was fit on, not the array being transformed. Thank you for the bug report @mfeurer, and sorry we somehow missed this in the implementation of
add_indicator_features. And apologies to @maniteja123 if you were led to believe this was correct operation.mfeurer commentedon May 7, 2016
@jnothman No problem. Should I work on this on Monday?
jnothman commentedon May 7, 2016
If you would like to. It may be easier for @maniteja123 to fix it up as the original implementer.
maniteja123 commentedon May 7, 2016
Hi, thanks @jnothman for clarifying. It is me who needs to apologise for misinterpreting the function of imputed feature indicator. Thanks @mfeurer for detailed explanation for the correct implementation. Please feel free to fix the issue but I would be glad to get it done if it fine by you. Thanks.
mfeurer commentedon May 7, 2016
I'm totally fine if you fix this @maniteja123
35 remaining items
maniteja123 commentedon Aug 10, 2016
Hi, sorry for the delay. I would be glad to complete the work on it. The tests are failing for the sparse case in some previous versions. I would be thankful if someone has more insight into the reason. I will try to fixing the errors. Besides the error, it would be helpful if the functionality is the expected one now. Thanks!
jnothman commentedon Aug 10, 2016
A design decision. When a feature has missing values in
transformbut not infitshould we:In a traditional fit/predict pipeline, some indicator features created by {2,3} may be constantly 0 at fit time, and so have no impact on the fitted model. However, these could benefit in a
partial_fitapproach... Option 4 actually allows the model to exploit knowledge of data absence in general, and indeed could be offered alongside any of the prior options.@mfeurer, @amueller, your opinions?
mfeurer commentedon Aug 10, 2016
I agree on your point why Option 1 is a bad idea and I think Option 4 is some kind of 'meta'-information which shouldn't be added by the imputer. My favourite is Option 3, with a fallback to Option 2.
mfeurer commentedon Aug 10, 2016
Deleted
jnothman commentedon Aug 10, 2016
I don't particularly like option 2, because certainly in the dense data case, you can waste a lot of space when only a few features are liable to be missing.
I actually do quite like option 4 -- I think its benefits from a learning perspective are most clear -- but maybe it needs another parameter,
add_global_indicator_feature(yuck name!) to switch on.Practically, I'd be happy to have option 1 or 2 implemented now by default in #6782, and then 3 and 4 can come through additional parameters later.
I don't know which test failures you're referring to.
mfeurer commentedon Aug 10, 2016
Five hours later, Option 1 makes more sense than Option 2 :)
Option 3 would then override Option 1, right?
I also deleted the previous comment, sorry for that.
jnothman commentedon Aug 10, 2016
Well it seems that I ask questions to then work out the answer for myself. But even better if there's consensus!
amueller commentedon Aug 23, 2016
How about failing on transform if indicator features are enabled and new features have missing values and no set was specified?
amueller commentedon Aug 23, 2016
I think we can leave the global for now. too many options :(
raghavrv commentedon Aug 25, 2016
I agree, option 4 is basically a compounded feature. Not sure if we want to do that at imputer?
Option 1 feels implicit...
Option 3 (provisioned via a list attr
missing_features) with a fallback to Option 2 (whenmissing_featuresisNone) looks good to me. It is more explicit.When the feature is a constant 0, most estimators ignore it. Maybe if you want it removed, we could add a
RemoveConstantFeaturetransformer?Option 4 is a separate feature request. Maybe should be raised as a separate issue?
raghavrv commentedon Aug 25, 2016
With no other option implemented to suppress that error, I think simply raising an error would give them no control other than to add dummy rows (to their
fitdata) with missing values in features that may go missing attransform.MechCoder commentedon Aug 30, 2016
Pretty late reaction but Ouch!
jnothman commentedon Aug 30, 2016
Having reverted the feature, this is no longer an issue. Thanks for the input; we'll work on building it better next time.
jnothman commentedon Aug 30, 2016
And thanks so much to @mfeurer for bringing this to our attention.