Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MAINT Clean deprecation for 1.2: feature names exact match #24660

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions sklearn/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,7 @@ def _check_feature_names(self, X, *, reset):
fitted_feature_names != X_feature_names
):
message = (
"The feature names should match those that were "
"passed during fit. Starting version 1.2, an error will be raised.\n"
"The feature names should match those that were passed during fit.\n"
)
fitted_feature_names_set = set(fitted_feature_names)
X_feature_names_set = set(X_feature_names)
Expand Down Expand Up @@ -449,7 +448,7 @@ def add_names(names):
"Feature names must be in the same order as they were in fit.\n"
)

warnings.warn(message, FutureWarning)
raise ValueError(message)

def _validate_data(
self,
Expand Down
2 changes: 1 addition & 1 deletion sklearn/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ def transform(self, X):
trans.fit(df)
msg = "The feature names should match those that were passed"
df_bad = pd.DataFrame(X_np, columns=iris.feature_names[::-1])
with pytest.warns(FutureWarning, match=msg):
with pytest.raises(ValueError, match=msg):
trans.transform(df_bad)

# warns when fitted on dataframe and transforming a ndarray
Expand Down
24 changes: 7 additions & 17 deletions sklearn/utils/estimator_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3913,22 +3913,14 @@ def check_dataframe_column_names_consistency(name, estimator_orig):
X_bad = pd.DataFrame(X, columns=invalid_name)

expected_msg = re.escape(
"The feature names should match those that were passed "
"during fit. Starting version 1.2, an error will be raised.\n"
"The feature names should match those that were passed during fit.\n"
f"{additional_message}"
)
for name, method in check_methods:
# TODO In 1.2, this will be an error.
with warnings.catch_warnings():
warnings.filterwarnings(
"error",
category=FutureWarning,
module="sklearn",
)
with raises(
FutureWarning, match=expected_msg, err_msg=f"{name} did not raise"
):
method(X_bad)
with raises(
ValueError, match=expected_msg, err_msg=f"{name} did not raise"
):
method(X_bad)

# partial_fit checks on second call
# Do not call partial fit if early_stopping is on
Expand All @@ -3942,10 +3934,8 @@ def check_dataframe_column_names_consistency(name, estimator_orig):
else:
estimator.partial_fit(X, y)

with warnings.catch_warnings():
warnings.filterwarnings("error", category=FutureWarning, module="sklearn")
with raises(FutureWarning, match=expected_msg):
estimator.partial_fit(X_bad, y)
with raises(ValueError, match=expected_msg):
estimator.partial_fit(X_bad, y)


def check_transformer_get_feature_names_out(name, transformer_orig):
Expand Down