Skip to content

Commit

Permalink
REF: Make _hstack a staticmethod on ColumnTransformer
Browse files Browse the repository at this point in the history
This lets subclasses re-use more of sklearn.compose._column_transformer.

xref dask/dask-ml#315
  • Loading branch information
TomAugspurger committed Jul 26, 2018
1 parent f1c9678 commit 59bb318
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions sklearn/compose/_column_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def fit_transform(self, X, y=None):
self._update_fitted_transformers(transformers)
self._validate_output(Xs)

return _hstack(list(Xs), self.sparse_output_)
return self._hstack(list(Xs), self.sparse_output_)

def transform(self, X):
"""Transform X separately by each transformer, concatenate results.
Expand Down Expand Up @@ -471,7 +471,25 @@ def transform(self, X):
# All transformers are None
return np.zeros((X.shape[0], 0))

return _hstack(list(Xs), self.sparse_output_)
return self._hstack(list(Xs), self.sparse_output_)

@staticmethod
def _hstack(X, sparse_):
"""
Stacks X horizontally.
Supports input types (X): list of
numpy arrays, sparse arrays and DataFrames
This is implemented as a staticmethod to enable subclasses to control
the stacking behavior, while reusing everything else from
ColumnTransformer.
"""
if sparse_:
return sparse.hstack(X).tocsr()
else:
X = [f.toarray() if sparse.issparse(f) else f for f in X]
return np.hstack(X)


def _check_key_type(key, superclass):
Expand Down Expand Up @@ -505,20 +523,6 @@ def _check_key_type(key, superclass):
return False


def _hstack(X, sparse_):
"""
Stacks X horizontally.
Supports input types (X): list of
numpy arrays, sparse arrays and DataFrames
"""
if sparse_:
return sparse.hstack(X).tocsr()
else:
X = [f.toarray() if sparse.issparse(f) else f for f in X]
return np.hstack(X)


def _get_column(X, key):
"""
Get feature column(s) from input data X.
Expand Down

0 comments on commit 59bb318

Please sign in to comment.