Skip to content

ENH Adds feature_names_in_ to ColumnTransformer #20839

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 5 commits into from
Aug 26, 2021
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
30 changes: 12 additions & 18 deletions sklearn/compose/_column_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,12 @@ def _iter(self, fitted=False, replace_strings=False, column_as_strings=False):
elif _is_empty_column_selection(columns):
continue

if column_as_strings and self._only_str_columns:
if column_as_strings:
# Convert all columns to using their string labels
columns_is_scalar = np.isscalar(columns)

indices = self._transformer_to_input_indices[name]
columns = self._feature_names_in[indices]
columns = self.feature_names_in_[indices]

if columns_is_scalar:
# selection is done with one dimension
Expand Down Expand Up @@ -383,13 +383,13 @@ def get_feature_names(self):
if trans == "drop" or _is_empty_column_selection(column):
continue
if trans == "passthrough":
if self._feature_names_in is not None:
if hasattr(self, "feature_names_in_"):
if (not isinstance(column, slice)) and all(
isinstance(col, str) for col in column
):
feature_names.extend(column)
else:
feature_names.extend(self._feature_names_in[column])
feature_names.extend(self.feature_names_in_[column])
else:
indices = np.arange(self._n_features)
feature_names.extend(["x%d" % i for i in indices[column]])
Expand Down Expand Up @@ -543,14 +543,8 @@ def fit_transform(self, X, y=None):
sparse matrices.

"""
# TODO: this should be `feature_names_in_` when we start having it
if hasattr(X, "columns"):
self._feature_names_in = np.asarray(X.columns)
self._only_str_columns = all(
isinstance(col, str) for col in self._feature_names_in
)
else:
self._feature_names_in = None
self._check_feature_names(X, reset=True)

X = _check_X(X)
# set n_features_in_ attribute
self._check_n_features(X, reset=True)
Expand Down Expand Up @@ -605,9 +599,9 @@ def transform(self, X):
check_is_fitted(self)
X = _check_X(X)

fit_dataframe_and_transform_dataframe = (
self._feature_names_in is not None and hasattr(X, "columns")
)
fit_dataframe_and_transform_dataframe = hasattr(
self, "feature_names_in_"
) and hasattr(X, "columns")

if fit_dataframe_and_transform_dataframe:
named_transformers = self.named_transformers_
Expand All @@ -622,7 +616,7 @@ def transform(self, X):
]

all_indices = set(chain(*non_dropped_indices))
all_names = set(self._feature_names_in[ind] for ind in all_indices)
all_names = set(self.feature_names_in_[ind] for ind in all_indices)

diff = all_names - set(X.columns)
if diff:
Expand Down Expand Up @@ -683,11 +677,11 @@ def _sk_visual_block_(self):
elif hasattr(self, "_remainder"):
remainder_columns = self._remainder[2]
if (
self._feature_names_in is not None
hasattr(self, "feature_names_in_")
and remainder_columns
and not all(isinstance(col, str) for col in remainder_columns)
):
remainder_columns = self._feature_names_in[remainder_columns].tolist()
remainder_columns = self.feature_names_in_[remainder_columns].tolist()
transformers = chain(
self.transformers, [("remainder", self.remainder, remainder_columns)]
)
Expand Down
18 changes: 18 additions & 0 deletions sklearn/compose/tests/test_column_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,3 +1595,21 @@ def test_get_feature_names_empty_selection(selector):
ct = ColumnTransformer([("ohe", OneHotEncoder(drop="first"), selector)])
ct.fit([[1, 2], [3, 4]])
assert ct.get_feature_names() == []


def test_feature_names_in_():
"""Feature names are stored in column transformer.

Column transfomer deliberately does not check for column name consistency.
It only checks that the non-dropped names seen in `fit` are seen
in `transform`. This behavior is already tested in
`test_feature_name_validation_missing_columns_drop_passthough`"""

pd = pytest.importorskip("pandas")

feature_names = ["a", "c", "d"]
df = pd.DataFrame([[1, 2, 3]], columns=feature_names)
ct = ColumnTransformer([("bycol", Trans(), ["a", "d"])], remainder="passthrough")

ct.fit(df)
assert_array_equal(ct.feature_names_in_, feature_names)