Skip to content

Imputer(with add_indicator_features=True) can create arrays of different shape in transform() than in fit_transform() #6767

Closed

Description

@mfeurer
from sklearn.preprocessing import Imputer
import numpy as np

X_train = np.array([[1, np.NaN], [1, 1]])
X_test = np.array([[1, 1], [1, 1]])

imputer = Imputer(add_indicator_features=True)
X_train_imp = imputer.fit_transform(X_train)
print(X_train_imp.shape)

X_test_imp = imputer.transform(X_test)
print(X_test_imp.shape)

Expected Results

(2, 3)
(2, 3)

Actual Results

(2, 3)
(2, 2)

Versions

Linux-3.13.0-54-generic-x86_64-with-Ubuntu-14.04-trusty
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4]
NumPy 1.11.0
SciPy 0.17.0
Scikit-Learn 0.18.dev0

Activity

  1. maniteja123 commented on May 6, 2016

    @maniteja123
    Contributor

    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.

  2. mfeurer commented on May 6, 2016

    @mfeurer
    Author

    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:

    estimator = Pipeline([("imputer", Imputer(missing_values=0,
    . Therefore, I think this behaviour is wrong, and transform() should always output the same amount of features as fit_transform().

  3. maniteja123 commented on May 6, 2016

    @maniteja123
    Contributor

    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.

  4. maniteja123 commented on May 6, 2016

    @maniteja123
    Contributor

    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.

  5. mfeurer commented on May 6, 2016

    @mfeurer
    Author

    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?

  6. 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
  7. mfeurer commented on May 6, 2016

    @mfeurer
    Author

    EDIT: I updated the title to make the issue more descriptive.

  8. maniteja123 commented on May 6, 2016

    @maniteja123
    Contributor

    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.

  9. jnothman commented on May 7, 2016

    @jnothman
    Member

    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.

  10. added
    Easy
    on May 7, 2016
  11. mfeurer commented on May 7, 2016

    @mfeurer
    Author

    @jnothman No problem. Should I work on this on Monday?

  12. jnothman commented on May 7, 2016

    @jnothman
    Member

    If you would like to. It may be easier for @maniteja123 to fix it up as the original implementer.

  13. maniteja123 commented on May 7, 2016

    @maniteja123
    Contributor

    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.

  14. mfeurer commented on May 7, 2016

    @mfeurer
    Author

    I'm totally fine if you fix this @maniteja123

  15. 35 remaining items

  16. maniteja123 commented on Aug 10, 2016

    @maniteja123
    Contributor

    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!

  17. jnothman commented on Aug 10, 2016

    @jnothman
    Member

    A design decision. When a feature has missing values in transform but not in fit should we:

    1. have indicator features only for those features with missing values in training
    2. have an indicator for all features
    3. allow the user to specify the set of features for an indicator and otherwise default to 1 or 2
    4. 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.

    @mfeurer, @amueller, your opinions?

  18. mfeurer commented on Aug 10, 2016

    @mfeurer
    Author

    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.

  19. mfeurer commented on Aug 10, 2016

    @mfeurer
    Author

    Deleted

  20. jnothman commented on Aug 10, 2016

    @jnothman
    Member

    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.

  21. mfeurer commented on Aug 10, 2016

    @mfeurer
    Author

    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.

  22. jnothman commented on Aug 10, 2016

    @jnothman
    Member

    Well it seems that I ask questions to then work out the answer for myself. But even better if there's consensus!

  23. amueller commented on Aug 23, 2016

    @amueller
    Member

    How about failing on transform if indicator features are enabled and new features have missing values and no set was specified?

  24. amueller commented on Aug 23, 2016

    @amueller
    Member

    I think we can leave the global for now. too many options :(

  25. raghavrv commented on Aug 25, 2016

    @raghavrv
    Member

    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?

  26. raghavrv commented on Aug 25, 2016

    @raghavrv
    Member

    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.

  27. MechCoder commented on Aug 30, 2016

    @MechCoder
    Member

    Pretty late reaction but Ouch!

  28. jnothman commented on Aug 30, 2016

    @jnothman
    Member

    Having reverted the feature, this is no longer an issue. Thanks for the input; we'll work on building it better next time.

  29. jnothman commented on Aug 30, 2016

    @jnothman
    Member

    And thanks so much to @mfeurer for bringing this to our attention.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugEasy

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions