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
Missing features removal with SimpleImputer #16426
For the problematic features, either keep their values if valid or impute the fill_value during transform. I suggest adding a new parameter to trigger this behaviour with a warning highlighting the referred features.
As I'm willing to implement this feature, I look forward advices.
Reacted by Mitar, Martin Becker, atpage, ImpriMed and Arief Saferman
Currently sklearn.impute.SimpleImputer silently removes features that are np.nan on every training sample.
That's a fairly surprising (and I think undocumented) behavior. Though I imagine keeping columns with all 0s (or other fill_value) is not very helpful either, for most use cases (putting aside API consistency).
TBH, I have mixed feeling between documenting it as is, fixing as a bug, or adding an optional parameter to switch between these two behaviors as you suggest. Maybe other reviewers have stronger options about it.
Regarding the over imputer, I meant the KNNImputer and IterativeImputer. I recall a discussion about this in the IterativeImputer. If we are going to change the behaviour it would need to be consistent. I did not have time to look at it yet.
chiming in to agree that my expected behavior in the original post is that the column with all np.nan should be passed through. Never would I expect the column to be silently dropped. Thanks to all for looking into this.
Maybe you're right that we should abide by the expectation that imputation
should do nothing to the order or set of features. I'm okay to change the
default behaviour over time. I had advocated for the dropping feature
approach because a zero-variance training feature is unhelpful in terms of
machine learning... but this behaviour is unhelpful in terms of software
development.
I am glad this got reopened, as I was commenting on #10671 about this as well.
I think I see value in columns being removed, because this assures the property that after calling imputer you do not have any missing values. This is an important precondition for many models so it is useful to know that you can call something (an imputer) and be assured it (the property that there are no missing values) holds after calling.
So despite having problems with this behavior, I think this should not be changed: I think both keeping a column with all nans is problematic, and also changing a default is problematic (because it breaks the important property). Keeping nans is problematic because it is still hard for one to know which column is the column which has been kept and not imputed. We are currently (because of this bug) trying to workaround by checking for all-nan columns before calling imputer, but it is fragile because any change in logic of the imputer will throw our detection off which means that we will miss some cases. This is tricky also because what is nan is configurable. If we just keep columns around, we would still have to have this workaround detection implemented.
I think a better, simpler, and backwards compatible solution is to simple expose a public attribute on the imputer, which after fitting tells how input columns map to output columns, based on column indices. So something like column_map_ which would be a dict from input column indices to output. If some column is dropped, the output column index would be None.
So one can after fitting inspect and quickly determine if any and which columns are being dropped.
Particularly, I don't think that "sometimes changing the schema, based on the input" is a role of an imputer. Sure, those missing values might cause problems downstream, but changing the schema may break the pipeline itself.
Nonetheless, the PR preserves the default behaviour and I'll add a is_missing_ field (like statistics_ ).
Particularly, I don't think that "sometimes changing the schema, based on the input" is a role of an imputer.
I think this is just two types of assertions we have here. One assertion (A) is "output schema is the same as input schema" and the other one (B) is "if input has missing values, output does not have them anymore". Now, the question is how those interact. If there were no missing values, then it is easy to say A should hold. If there were some missing values, A can still hold. The question is what to do if there are just missing values. Which of two assertions should prevail. A or B in that case?
You are saying that imputer's job is not for B to prevail, but for A to ensured for any reason. But I think it is hard to not claim also the opposite, that the nature of the imputer is to assure B, even if it is at the cost of A.
is_missing_
This could also be a way to do it, but can you be more precise what you mean with this and what exactly would be semantic of it, and what would be stored there?
One assertion (A) is "output schema is the same as input schema"
Yes, I advocate for this one, because:
Different imputation strategies may have distinct undefined behaviours
Feature housekeeping and policies are indeed important, but are more specific to each problem: e.g. some algorithms may fail with nans, other may use it directly
Regarding the is_missing_ field, the user may use it to construct the mapping, but it is intrinsically a mask:
I don't see how this leading to a user friendly API, and I don't see why we
can't have an option to uphold both properties A and B by imputing empty
columns with a constant value.
Reacted by Micah Smith, Roman Yurchak, slavaradune and Ben Reiniger
I don't see how this leading to a user friendly API, and I don't see why we
can't have an option to uphold both properties A and B by imputing empty
columns with a constant value.
+1 And if one doesn't desire B then one could always pass fill_missing_features=np.nan (or any other paceholder for missing values) in #16695 to keep a columns with missing values and do something with it later.
If one anyway wants to drop the column with all all missing values (but just wants to know what is being dropped) I do not think it is reasonable to first fill it with a constant just to be dropped.
But I do not mind details here. What I really would like to see is an attribute like is_empty_ which tells which columns have been a special case (dropped, filled as a constant, left as is, whatever).
Another point on this in favour of NOT dropping columns of all NaNs at fit time is that it makes calculation of output feature names (via get_feature_names_out) possible pre-fit, assuming we pass in a valid input_features.
Activity
rth commentedon Feb 11, 2020
Thanks for the report!
That's a fairly surprising (and I think undocumented) behavior. Though I imagine keeping columns with all 0s (or other
fill_value) is not very helpful either, for most use cases (putting aside API consistency).It's also seems dependent on the strategy,
TBH, I have mixed feeling between documenting it as is, fixing as a bug, or adding an optional parameter to switch between these two behaviors as you suggest. Maybe other reviewers have stronger options about it.
jnothman commentedon Feb 12, 2020
glemaitre commentedon Feb 14, 2020
This behaviour ring the bell. We should check the behaviour across the different imputers.
vitorsrg commentedon Feb 17, 2020
The expected behaviour could be to keep the
NaN, so we can use anotherSimpleImputer(strategy='constant')after trying to impute with the mean:glemaitre commentedon Feb 17, 2020
Regarding the over imputer, I meant the
KNNImputerandIterativeImputer. I recall a discussion about this in theIterativeImputer. If we are going to change the behaviour it would need to be consistent. I did not have time to look at it yet.micahjsmith commentedon Feb 21, 2020
chiming in to agree that my expected behavior in the original post is that the column with all
np.nanshould be passed through. Never would I expect the column to be silently dropped. Thanks to all for looking into this.jnothman commentedon Feb 23, 2020
jnothman commentedon Feb 23, 2020
glemaitre commentedon Feb 23, 2020
FYI, the behaviour is consistent across imputers:
If we are going to change the behaviour, it needs to be shared between all imputers.
3 remaining items
mitar commentedon Apr 22, 2020
I am glad this got reopened, as I was commenting on #10671 about this as well.
I think I see value in columns being removed, because this assures the property that after calling imputer you do not have any missing values. This is an important precondition for many models so it is useful to know that you can call something (an imputer) and be assured it (the property that there are no missing values) holds after calling.
So despite having problems with this behavior, I think this should not be changed: I think both keeping a column with all nans is problematic, and also changing a default is problematic (because it breaks the important property). Keeping nans is problematic because it is still hard for one to know which column is the column which has been kept and not imputed. We are currently (because of this bug) trying to workaround by checking for all-nan columns before calling imputer, but it is fragile because any change in logic of the imputer will throw our detection off which means that we will miss some cases. This is tricky also because what is nan is configurable. If we just keep columns around, we would still have to have this workaround detection implemented.
I think a better, simpler, and backwards compatible solution is to simple expose a public attribute on the imputer, which after fitting tells how input columns map to output columns, based on column indices. So something like
column_map_which would be a dict from input column indices to output. If some column is dropped, the output column index would beNone.So one can after fitting inspect and quickly determine if any and which columns are being dropped.
vitorsrg commentedon Apr 22, 2020
Particularly, I don't think that "sometimes changing the schema, based on the input" is a role of an imputer. Sure, those missing values might cause problems downstream, but changing the schema may break the pipeline itself.
Nonetheless, the PR preserves the default behaviour and I'll add a
is_missing_field (likestatistics_).mitar commentedon Apr 22, 2020
I think this is just two types of assertions we have here. One assertion (A) is "output schema is the same as input schema" and the other one (B) is "if input has missing values, output does not have them anymore". Now, the question is how those interact. If there were no missing values, then it is easy to say A should hold. If there were some missing values, A can still hold. The question is what to do if there are just missing values. Which of two assertions should prevail. A or B in that case?
You are saying that imputer's job is not for B to prevail, but for A to ensured for any reason. But I think it is hard to not claim also the opposite, that the nature of the imputer is to assure B, even if it is at the cost of A.
This could also be a way to do it, but can you be more precise what you mean with this and what exactly would be semantic of it, and what would be stored there?
vitorsrg commentedon Apr 22, 2020
Yes, I advocate for this one, because:
Regarding the
is_missing_field, the user may use it to construct the mapping, but it is intrinsically a mask:mitar commentedon Apr 22, 2020
Let's maybe name it
is_empty_.jnothman commentedon Apr 23, 2020
rth commentedon Apr 23, 2020
+1 And if one doesn't desire B then one could always pass
fill_missing_features=np.nan(or any other paceholder for missing values) in #16695 to keep a columns with missing values and do something with it later.mitar commentedon Apr 24, 2020
If one anyway wants to drop the column with all all missing values (but just wants to know what is being dropped) I do not think it is reasonable to first fill it with a constant just to be dropped.
But I do not mind details here. What I really would like to see is an attribute like
is_empty_which tells which columns have been a special case (dropped, filled as a constant, left as is, whatever).hsorsky commentedon Sep 6, 2022
Another point on this in favour of NOT dropping columns of all NaNs at fit time is that it makes calculation of output feature names (via
get_feature_names_out) possible pre-fit, assuming we pass in a validinput_features.ENH keep features with all missing values during imputation (#24770)