Skip to content
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

Alters feature 'combining' behavior, see issue #51. #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 12 additions & 9 deletions sklearn_pandas/dataframe_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def _build_transformer(transformers):
return transformers


def _sparse_to_dense(extracted):
return [x.toarray() if sparse.issparse(x) else x for x in extracted]


class DataFrameMapper(BaseEstimator, TransformerMixin):
"""
Map Pandas data frame column subsets to their own
Expand Down Expand Up @@ -120,13 +124,12 @@ def transform(self, X):

# If any of the extracted features is sparse, combine sparsely.
# Otherwise, combine as normal arrays.
if any(sparse.issparse(fea) for fea in extracted):
stacked = sparse.hstack(extracted).tocsr()
# return a sparse matrix only if the mapper was initialized
# with sparse=True
if not self.sparse:
stacked = stacked.toarray()
if self.sparse:
if any(sparse.issparse(fea) for fea in extracted):
# fails if array in extracted has dtype=object
return sparse.hstack(extracted).tocsr()
else:
# convert to sparse
return sparse.csr_matrix(np.hstack(extracted))
else:
stacked = np.hstack(extracted)

return stacked
return np.hstack(_sparse_to_dense(extracted))