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

Improve the performance of from_pandas in the case of low-cardinality categoricals #275

Merged
merged 2 commits into from
Jul 18, 2023
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
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
Changelog
=========

Unreleased
----------

**Other changes:**

- Improve the performance of ``from_pandas`` in the case of low-cardinality categorical variables.

3.1.10 - 2023-06-23
-------------------

Expand Down
12 changes: 2 additions & 10 deletions src/tabmat/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,15 @@ def from_pandas(
if object_as_cat and coldata.dtype == object:
coldata = coldata.astype("category")
if isinstance(coldata.dtype, pd.CategoricalDtype):
cat = CategoricalMatrix(coldata, drop_first=drop_first, dtype=dtype)
if len(coldata.cat.categories) < cat_threshold:
(
X_dense_F,
X_sparse,
dense_indices,
sparse_indices,
) = _split_sparse_and_dense_parts(
pd.get_dummies(
coldata,
prefix=colname,
sparse=True,
drop_first=drop_first,
dtype=np.float64,
)
.sparse.to_coo()
.tocsc(),
sps.csc_matrix(cat.tocsr(), dtype=dtype),
threshold=sparse_threshold,
)
matrices.append(X_dense_F)
Expand All @@ -103,7 +96,6 @@ def from_pandas(
indices.append(sparse_indices)

else:
cat = CategoricalMatrix(coldata, drop_first=drop_first, dtype=dtype)
matrices.append(cat)
is_cat.append(True)
if cat_position == "expand":
Expand Down
Loading