ENH Make _hstack a method inside ColumnTransformer
This lets subclasses re-use more of sklearn.compose._column_transformer.
xref
@@ -443,7 +443,7 @@ boolean mask array or callable
self._update_fitted_transformers(transformers)
self._validate_output(Xs)
- return _hstack(list(Xs), self.sparse_output_)
+ return self._hstack(list(Xs))
def transform(self, X):
"""Transform X separately by each transformer, concatenate results.
@@ -471,7 +471,23 @@ boolean mask array or callable
# All transformers are None
return np.zeros((X.shape[0], 0))
- return _hstack(list(Xs), self.sparse_output_)
+ return self._hstack(list(Xs))
+
+ def _hstack(self, Xs):
+ """Stacks Xs horizontally.
+
+ This allows subclasses to control the stacking behavior, while reusing
+ everything else from ColumnTransformer.
+
+ Parameters
+ ----------
+ Xs : List of numpy arrays, sparse arrays, or DataFrames
+ """
+ if self.sparse_output_:
+ return sparse.hstack(Xs).tocsr()
+ else:
+ Xs = [f.toarray() if sparse.issparse(f) else f for f in Xs]
+ return np.hstack(Xs)
def _check_key_type(key, superclass):
@@ -505,20 +521,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.
