-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG] FIX ColumnTransformer: raise error on reordered columns with remainder #14237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
adrinjalali
merged 19 commits into
scikit-learn:master
from
schuderer:coltrans_fix_remaining_reordered_cols
Jul 22, 2019
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6cfc722
FIX Raise error on reordered columns in ColumnTransformer with remainder
schuderer d06b27a
FIX Check for different length of X.columns to avoid exception
schuderer 16cef75
FIX linter, line too long
schuderer 4feb366
Merge remote-tracking branch 'upstream/master' into coltrans_fix_rema…
schuderer 6421cb1
FIX import _check_key_type from its new location utils
schuderer 8d2a18d
Merge remote-tracking branch 'upstream/master' into coltrans_fix_rema…
schuderer d709be1
ENH Adjust doc, allow added columns
schuderer 096b58d
Merge remote-tracking branch 'upstream/master' into coltrans_fix_rema…
schuderer 8bfc3ce
Fix comment typo as suggested, remove non-essential exposition in doc
schuderer d4ae600
Merge remote-tracking branch 'upstream/master' into coltrans_fix_rema…
schuderer 47b645d
Add PR 14237 to what's new
schuderer 39360de
Merge remote-tracking branch 'upstream/master' into coltrans_fix_rema…
schuderer 3b404d8
Avoid AttributeError in favor of ValueError "column names only for DF"
schuderer 011a2a2
ENH Add check for n_features_ for array-likes and DataFrames
schuderer c61143c
Rename self.n_features to self._n_features
schuderer 5f0b92b
Replaced backslash line continuation with parenthesis
schuderer d8e1fc0
Merge remote-tracking branch 'upstream/master' into coltrans_fix_rema…
schuderer e1434c2
Merge remote-tracking branch 'upstream/master' into coltrans_fix_rema…
schuderer 6c28e4d
Style changes
schuderer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will raise an error if
X
is a numpy array. Check ifX
is a pandas array before heading into this path?In future PR, we can use
_df_columns
to signal thatX
was a dataframe duringfit
and raise an error, if duringtransform
X
is not a dataframe.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good point, @thomasjpfan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks! Duck-typing check ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, though ideally we should also be checking the number of columns when X is not a DataFrame. Not sure if that needs to be in this pull request
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8000Interpreting the last suggestion loosely, I looked into also checking column order for structured numpy arrays (np arrays with named columns) in an analogous way as now for DataFrames and I found that this would change the PR a lot. ColumnTransformer currently explicitly does not allow named columns for non-DataFrames and raises an error if one tries. I now think that this was not the intended suggestion, but wanted to clearly state my (maybe misguided) interpretations. If this train of thought is relevant, we can continue this discussion in RFC #14251.
When reading the suggestion literally, i.e. only checking the number of columns for non-DataFrames -- this condition is only in the code originally to avoid an exception when referencing X.columns. If this check is a goal of the PR itself, I can change the conditionals (and it would become a somewhat separate check with a separate error message as
_df_columns
only exists for DataFrames combined with column names). I can gladly do this if it's found useful.For now, I'll only commit the suggested code change from my previous comment (plus test assertion), but that does not mean that I wouldn't do the other one at all. I'll wait for a yay or nay on the numpy-num-of-cols-check. :)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PS to the commit: Note that when passing an np array to transform, an error will still be raised -- not here, but in the correct location (ColumnTransformer checks if named columns are used with non-DataFrames and raises a ValueError if this is not the case).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We explicitly decided not to support struct arrays. The proposal here was to make sure we continue to allow fitting on DataFrame and transforming on numpy array, if that is currently supported.
The check for the number of columns in an array is because we provide that security in every other estimator, raising an error if the input has changed shape. Here it is also applicable but not currently done. It's not really necessary for this pull request
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jnothman! I interpret your last sentence as you leaving it for me to decide. :)
I added a general
self.n_features_
check in analogy to the one herescikit-learn/sklearn/ensemble/bagging.py
Line 682 in 7b8cbc8
Referencing relevant PR #13603 as
ColumnTransformer
now also hasn_features_
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Things get a little hairy here. I feel like for numpy arrays we should not be that lenient, since they are arrays not columnar frames...
But I can accept this for the version 0.20-21 fix.
Similarly, since I'm trying to include this in a patch release, make this
_n_features
to keep the patch API compatible. This also avoids making more deprecation work in #13603 or subsequent to it.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, if it is acceptable as it is for this fix, I'm leaving it unchanged (both arrays as well as DFs are checked leniently, accepting added columns, but not removed columns). That way I'm also confident that this PR doesn't break any relied-on behavior. Looking forward to other PRs like #14251 and #13603 for helping in making checks more obvious for first-time contributors like me (and users, of course). I found this PR to be quite the unexpected rabbit hole: every time I thought I understood how the pieces work together, there was still more to discover. :D I learned a lot, though. 👍 Hope than this PR is still a net win for the maintainers regarding time spent vs contribution, despite the communication overhead and my occasional chattiness.
I see, thanks! I renamed it to
_n_features
. This is the only change in the latest commit.