8000 FIX Only raise feature name warning with mixed types and strings by thomasjpfan · Pull Request #22410 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

FIX Only raise feature name warning with mixed types and strings #22410

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 4 commits into from
Feb 8, 2022
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
6 changes: 6 additions & 0 deletions doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ random sampling procedures.
`force_finite=False` if you really want to get non-finite values and keep
the old behavior.

- |Fix| Panda's DataFrames with all non-string columns such as a MultiIndex no
longer warns when passed into an Estimator. Estimators will continue to
ignore the column names in DataFrames with non-string columns. For
`feature_names_in_` to be defined, columns must be all strings. :pr:`22410` by
`Thomas Fan`_.

Changelog
---------

Expand Down
13 changes: 8 additions & 5 deletions sklearn/utils/tests/test_validation.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -1613,11 +1613,14 @@ def test_check_array_deprecated_matrix():

@pytest.mark.parametrize(
"names",
[list(range(2)), range(2), None],
ids=["list-int", "range", "default"],
[list(range(2)), range(2), None, [["a", "b"], ["c", "d"]]],
ids=["list-int", "range", "default", "MultiIndex"],
)
def test_get_feature_names_pandas_with_ints_no_warning(names):
"""Get feature names with pandas dataframes with ints without warning"""
"""Get feature names with pandas dataframes without warning.

Column names with consistent dtypes will not warn, such as int or MultiIndex.
"""
pd = pytest.importorskip("pandas")
X = pd.DataFrame([[1, 2], [4, 5], [5, 6]], columns=names)

Expand Down Expand Up @@ -1648,10 +1651,10 @@ def test_get_feature_names_numpy():
@pytest.mark.parametrize(
"names, dtypes",
[
([["a", "b"], ["c", "d"]], "['tuple']"),
(["a", 1], "['int', 'str']"),
(["pizza", ["a", "b"]], "['list', 'str']"),
],
ids=["multi-index", "mixed"],
ids=["int-str", "list-str"],
)
def test_get_feature_names_invalid_dtypes_warns(names, dtypes):
"""Get feature names warns when the feature names have mixed dtypes"""
Expand Down
7 changes: 3 additions & 4 deletions sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,9 +1810,8 @@ def _get_feature_names(X):

types = sorted(t.__qualname__ for t in set(type(v) for v in feature_names))

# Warn when types are mixed.
# ints and strings do not warn
if len(types) > 1 or not (types[0].startswith("int") or types[0] == "str"):
# Warn when types are mixed and string is one of the types
if len(types) > 1 and "str" in types:
# TODO: Convert to an error in 1.2
warnings.warn(
"Feature names only support names that are all strings. "
Expand All @@ -1823,7 +1822,7 @@ def _get_feature_names(X):
return

# Only feature names of all strings are supported
if types[0] == "str":
if len(types 4946 ) == 1 and types[0] == "str":
return feature_names


Expand Down
0