-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
ColumnTransformer input feature name and count validation #14544
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3a69d12
deprecate non matching input in column transformer, during fit and tr…
adrinjalali adc27b8
remove util func, fix tests
adrinjalali 24b9b46
pep8
adrinjalali 79e8b18
fix more tests
adrinjalali b5a42c6
merge upstream/master
adrinjalali 52554df
error on negative indexing and unequal number of columns
adrinjalali 0e3b543
Merge remote-tracking branch 'upstream/master' into ct/strict
adrinjalali fa2ff2d
test slice, move function
adrinjalali 93bc99e
Merge remote-tracking branch 'upstream/master' into ct/strict
adrinjalali 2b2b7e3
address comments
adrinjalali 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
""" | ||
import re | ||
|
||
import warnings | ||
import numpy as np | ||
from scipy import sparse | ||
import pytest | ||
|
@@ -498,7 +499,10 @@ def test_column_transformer_invalid_columns(remainder): | |
ct = ColumnTransformer([('trans', Trans(), col)], remainder=remainder) | ||
ct.fit(X_array) | ||
X_array_more = np.array([[0, 1, 2], [2, 4, 6], [3, 6, 9]]).T | ||
ct.transform(X_array_more) # Should accept added columns | ||
msg = ("Given feature/column names or counts do not match the ones for " | ||
"the data given during fit.") | ||
with pytest.warns(DeprecationWarning, match=msg): | ||
ct.transform(X_array_more) # Should accept added columns, for now | ||
X_array_fewer = np.array([[0, 1, 2], ]).T | ||
err_msg = 'Number of features' | ||
with pytest.raises(ValueError, match=err_msg): | ||
|
@@ -1096,13 +1100,16 @@ def test_column_transformer_reordered_column_names_remainder(explicit_colname): | |
|
||
tf.fit(X_fit_df) | ||
err_msg = 'Column ordering must be equal' | ||
warn_msg = ("Given feature/column names or counts do not match the ones " | ||
"for the data given during fit.") | ||
with pytest.raises(ValueError, match=err_msg): | ||
tf.transform(X_trans_df) | ||
|
||
# No error for added columns if ordering is identical | ||
X_extended_df = X_fit_df.copy() | ||
X_extended_df['third'] = [3, 6, 9] | ||
tf.transform(X_extended_df) # No error should be raised | ||
with pytest.warns(DeprecationWarning, match=warn_msg): | ||
tf.transform(X_extended_df) # No error should be raised, for now | ||
|
||
# No 'columns' AttributeError when transform input is a numpy array | ||
X_array = X_fit_array.copy() | ||
|
@@ -1111,6 +1118,56 @@ def test_column_transformer_reordered_column_names_remainder(explicit_colname): | |
tf.transform(X_array) | ||
|
||
|
||
def test_feature_name_validation(): | ||
adrinjalali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Tests if the proper warning/error is raised if the columns do not match | ||
during fit and transform.""" | ||
pd = pytest.importorskip("pandas") | ||
|
||
X = np.ones(shape=(3, 2)) | ||
X_extra = np.ones(shape=(3, 3)) | ||
df = pd.DataFrame(X, columns=['a', 'b']) | ||
8000 | df_extra = pd.DataFrame(X_extra, columns=['a', 'b', 'c']) | |
|
||
tf = ColumnTransformer([('bycol', Trans(), ['a', 'b'])]) | ||
tf.fit(df) | ||
|
||
msg = ("Given feature/column names or counts do not match the ones for " | ||
"the data given during fit.") | ||
with pytest.warns(DeprecationWarning, match=msg): | ||
tf.transform(df_extra) | ||
|
||
tf = ColumnTransformer([('bycol', Trans(), [0])]) | ||
tf.fit(df) | ||
|
||
with pytest.warns(DeprecationWarning, match=msg): | ||
tf.transform(X_extra) | ||
|
||
with warnings.catch_warnings(record=True) as warns: | ||
tf.transform(X) | ||
assert not warns | ||
|
||
tf = ColumnTransformer([('bycol', Trans(), ['a'])], | ||
remainder=Trans()) | ||
tf.fit(df) | ||
with pytest.warns(DeprecationWarning, match=msg): | ||
tf.transform(df_extra) | ||
|
||
tf = ColumnTransformer([('bycol', Trans(), [0, -1])]) | ||
adrinjalali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tf.fit(df) | ||
msg = "At least one negative column was used to" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also check that no warning is raised when n_features matches with negative indexing? Just in case. |
||
with pytest.raises(RuntimeError, match=msg): | ||
tf.transform(df_extra) | ||
|
||
tf = ColumnTransformer([('bycol', Trans(), slice(-1, -3, -1))]) | ||
tf.fit(df) | ||
with pytest.raises(RuntimeError, match=msg): | ||
tf.transform(df_extra) | ||
|
||
with warnings.catch_warnings(record=True) as warns: | ||
tf.transform(df) | ||
assert not warns | ||
|
||
|
||
@pytest.mark.parametrize("array_type", [np.asarray, sparse.csr_matrix]) | ||
def test_column_transformer_mask_indexing(array_type): | ||
# Regression test for #14510 | ||
|
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.