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
When building a pipeline which contains an imputer and a one hot encoder with an array marking the categorical features, the imputer can remove columns (in case they're completely missing) which makes the one hot encoder crash.
Not 100% sure, but either no exception or an argument to the imputer which configures the behavior in such cases (similar to one hot encoding where one can specify how to handle unknowns).
what should we return when imputing a column of all NaNs? would it be
better if OneHotEncoder just handled NaNs by leaving that row empty?
avoiding categorical_features by using something like the proposed
ColumnTransformer might also be a preferred structure...
In that case the result of the Imputer (with default params) is [[1.] [1.]] of shape (2, 1) which forces the mentioned exception in the OneHotEncoder. The nan columns will be dropped imputation.py#L340-L341.
@jnothman the issue is not how the one hot encoder does not handle missing values, but that it expects more features than there will be present due to the imputer dropping one.
How would this look like with the proposed ColumnTransformer? Like this?
If yes, the problem might actually already be solved in the master branch by employing a different construct in the pipeline.
However, it could still be a good idea to not drop the column in the Imputer(), but by making the dropping explicit by a VarianceThreshold()? Thanks a lot @nok for going ahead and creating a PR for this.
@mfeurer How do you like the ColumnTransformer solution? It also allows different strategies for imputation and scaling on the categorical and numerical features, which I think is something you usually want.
This turned out to be quite a problem for us. The issue is that if imputer deletes columns then it is not really possible to map input columns to output columns.
I would ask that this is reopened and that there is some way for imputer to communicate which columns have been removed. Not sure what would be the best API for this though.
Reacted by Ife Anyansi, Dmitry Bochkov, Tamer Salama, evalparse, irodionzaytsev, Ben Reiniger and Karim
I would ask that this is reopened and that there is some way for imputer to communicate which columns have been removed. Not sure what would be the best API for this though.
Thanks for the feedback @mitar . Could you comment why the above solution using ColumnTransformer transformer doesn't work for you? In the long term this should also be solved by propagating feature names (likely in 0.24).
This is why I hate sklearn
@irodionzaytsev This is not an acceptable way of communicating on the issue tracker. Please be constructive.
Could you comment why the above solution using ColumnTransformer transformer doesn't work for you?
It does not work for us, because our problem is not with one-hot encoding, but with simply knowing which data comes from where. In our case we have metadata attached to every column. So if put X categorical columns into a ColumnTransformer and apply Imputer currently it is not necessary that X columns are returned, so we do not know how to map metadata to output columns. So ColumnTransformer does not really help in our case. That is just a workaround for one-hot encoding to not crash. But if you are doing anything else, it is still problematic.
In the long term this should also be solved by propagating feature names (likely in 0.24).
Yea, I heard about that solution, but the problem there is that Pandas DataFrame allows non-unique column names. So what happens if such DataFrame is used an input?
Yes, I agree that even if feature names are implemented it's still a behavior that can lead to issues in the code, and there should be an easy way to opt-out.
Let's continue the discussion in #16426 that's about the same issue, there is also an associated PR that looks to be in a good shape.
Activity
jnothman commentedon Feb 22, 2018
nok commentedon Feb 22, 2018
In that case the result of the Imputer (with default params) is
[[1.] [1.]]of shape(2, 1)which forces the mentioned exception in the OneHotEncoder. Thenancolumns will be dropped imputation.py#L340-L341.nok commentedon Feb 23, 2018
I added an initial PR for a discussion and further suggestions.
mfeurer commentedon Feb 23, 2018
@jnothman the issue is not how the one hot encoder does not handle missing values, but that it expects more features than there will be present due to the imputer dropping one.
How would this look like with the proposed
ColumnTransformer? Like this?If yes, the problem might actually already be solved in the master branch by employing a different construct in the pipeline.
However, it could still be a good idea to not drop the column in the
Imputer(), but by making the dropping explicit by aVarianceThreshold()? Thanks a lot @nok for going ahead and creating a PR for this.amueller commentedon Feb 26, 2018
@mfeurer How do you like the
ColumnTransformersolution? It also allows different strategies for imputation and scaling on the categorical and numerical features, which I think is something you usually want.mfeurer commentedon Feb 27, 2018
I'm fine with that solution as well, great that it's already solved. And having different imputation will actually be a good thing!
nok commentedon Feb 27, 2018
Nice solution, thanks for your feedback @amueller . So we can take the open PR #10678 back I guess?
mitar commentedon Feb 28, 2019
This turned out to be quite a problem for us. The issue is that if imputer deletes columns then it is not really possible to map input columns to output columns.
I would ask that this is reopened and that there is some way for imputer to communicate which columns have been removed. Not sure what would be the best API for this though.
irodionzaytsev commentedon Apr 20, 2020
rth commentedon Apr 20, 2020
Thanks for the feedback @mitar . Could you comment why the above solution using
ColumnTransformertransformer doesn't work for you? In the long term this should also be solved by propagating feature names (likely in 0.24).@irodionzaytsev This is not an acceptable way of communicating on the issue tracker. Please be constructive.
mitar commentedon Apr 21, 2020
It does not work for us, because our problem is not with one-hot encoding, but with simply knowing which data comes from where. In our case we have metadata attached to every column. So if put X categorical columns into a
ColumnTransformerand applyImputercurrently it is not necessary that X columns are returned, so we do not know how to map metadata to output columns. SoColumnTransformerdoes not really help in our case. That is just a workaround for one-hot encoding to not crash. But if you are doing anything else, it is still problematic.Yea, I heard about that solution, but the problem there is that Pandas DataFrame allows non-unique column names. So what happens if such DataFrame is used an input?
rth commentedon Apr 21, 2020
Yes, I agree that even if feature names are implemented it's still a behavior that can lead to issues in the code, and there should be an easy way to opt-out.
Let's continue the discussion in #16426 that's about the same issue, there is also an associated PR that looks to be in a good shape.
mitar commentedon Apr 21, 2020
Thanks!