8000 Allowed trans='passthrough' to handle scalar column input. by lrjball · Pull Request #14495 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content
< 8000 div hidden="hidden" data-view-component="true" class="js-stale-session-flash stale-session-flash flash flash-warn flash-full"> Dismiss alert

Allowed trans='passthrough' to handle scalar column input. #14495

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

Closed
wants to merge 4 commits into from
Closed
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
17 changes: 15 additions & 2 deletions sklearn/compose/_column_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,9 @@ def _iter(self, fitted=False, replace_strings=False):
# skip in case of 'drop'
if trans == 'passthrough':
trans = FunctionTransformer(
accept_sparse=True, check_inverse=False
)
func=_passthrough_func,
accept_sparse=True,
check_inverse=False)
elif trans == 'drop':
continue
elif _is_empty_column_selection(column):
Expand Down Expand Up @@ -707,3 +708,15 @@ def make_column_transformer(*transformers, **kwargs):
remainder=remainder,
sparse_threshold=sparse_threshold,
verbose=verbose)


def _passthrough_func(X):
"""
Function used in the FunctionTransformer for 'passthrough' columns
to ensure the correct shape.
"""
if not getattr(X, 'ndim', 0) == 2:
if hasattr(X, 'values'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't simply X = np.asarray(X) be better here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better but it's unclear to me whether either is good ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X = np.asarray(X) would work on anything that has __array__ (NamedArray for instance), but values is specific to pandas.

< 8000 /task-lists>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also values does a copy.

X = X.values
return X.reshape(-1, 1)
return X
43 changes: 43 additions & 0 deletions sklearn/compose/tests/test_column_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,49 @@ def test_column_transformer_special_strings():
ct.fit, X_array)


def test_column_transformer_passthrough_transform():
# check that when trans='passthrough', both lists and scalars can be passed
X_array = np.array([[0, 1, 2], [2, 4, 6]]).T
ct = ColumnTransformer([('pass', 'passthrough', 0)],
remainder='passthrough')
X_pass = ct.fit_transform(X_array)
assert_array_equal(X_pass, X_array)

ct = ColumnTransformer([('pass', 'passthrough', [0])],
remainder='passthrough')
X_pass = ct.fit_transform(X_array)
assert_array_equal(X_pass, X_array)

ct = ColumnTransformer([('pass', 'passthrough', [0, 1])])
X_pass = ct.fit_transform(X_array)
assert_array_equal(X_pass, X_array)

pd = pytest.importorskip('pandas')
df = pd.DataFrame(X_array, columns=['col0', 'col1'])

ct = ColumnTransformer([('pass', 'passthrough', 0)],
remainder='passthrough')
X_pass = ct.fit_transform(df)
assert_array_equal(X_pass, df.values)

ct = ColumnTransformer([('pass', 'passthrough', [0])],
remainder='passthrough')
X_pass = ct.fit_transform(df)
assert_array_equal(X_pass, df.values)

ct = ColumnTransformer([('pass', 'passthrough', [0, 1])])
X_pass = ct.fit_transform(df)
assert_array_equal(X_pass, df.values)

ct = ColumnTransformer([('pass', 'passthrough', 'col0')])
X_pass = ct.fit_transform(df)
assert_array_equal(X_pass, df[['col0']].values)

ct = ColumnTransformer([('pass', 'passthrough', ['col1', 'col0'])])
X_pass = ct.fit_transform(df)
assert_array_equal(X_pass, df[['col1', 'col0']].values)


def test_column_transformer_remainder():
X_array = np.array([[0, 1, 2], [2, 4, 6]]).T

Expand Down
0