-
Notifications
You must be signed in to change notification settings - Fork 5
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
pandas to matrix function #16
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0abf742
working prototype
MarcAntoineSchmidtQC 1d8bee7
more efficient implementation + docstring
MarcAntoineSchmidtQC 875f466
added simple test
MarcAntoineSchmidtQC f36d2c0
Merge branch 'master' into pd_np_to_matrix
MarcAntoineSchmidtQC bcc840a
keep ordering
MarcAntoineSchmidtQC ad8e9e3
fix test
MarcAntoineSchmidtQC b658a68
let user choose categorical location
MarcAntoineSchmidtQC 29586bf
typo
MarcAntoineSchmidtQC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import warnings | ||
|
||
import pandas as pd | ||
import scipy.sparse as sps | ||
|
||
from .categorical_matrix import CategoricalMatrix | ||
from .matrix_base import MatrixBase | ||
from .split_matrix import SplitMatrix, csc_to_split | ||
|
||
|
||
def from_pandas( | ||
df: pd.DataFrame, | ||
sparse_threshold: float = 0.1, | ||
cat_threshold: int = 4, | ||
object_as_cat: bool = False, | ||
) -> MatrixBase: | ||
""" | ||
TODO: | ||
- docstring | ||
- tests | ||
- efficiency | ||
- consider changing filename | ||
""" | ||
if object_as_cat: | ||
for colname in df.select_dtypes("object"): | ||
df[colname] = df[colname].astype("category") | ||
else: | ||
if not df.select_dtypes(include=object).empty: | ||
warnings.warn("DataFrame contains columns with object dtypes. Ignoring") | ||
|
||
categorical_component = df.select_dtypes(include=pd.CategoricalDtype) | ||
MarcAntoineSchmidtQC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
X_cat = [] | ||
for colname in categorical_component: | ||
X_cat.append(CategoricalMatrix(categorical_component[colname])) | ||
|
||
numerical_component = df.select_dtypes(include="number") | ||
MarcAntoineSchmidtQC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
X_noncat = csc_to_split(sps.csc_matrix(numerical_component)) | ||
|
||
return SplitMatrix([*X_noncat.matrices, *X_cat]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we support both sparse and dense DataFrames?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sparse meaning a data frame that consists of
pd.SparseArray
columns? That sounds useful 😉If you have a Pandas data frame that only consists of sparse arrays, you can use the sparse accessor to convert that data frame to a scipy sparse matrix using
X.sparse.to_coo()
. Note that the.sparse
accessor only can do.to_coo()
, so if you want something else, you then need to doto_csr()
orto_csc()
afterwards.Since this only works on data frames for which all columns are sparse,the SplitMatrix functionality is coming in very handy here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, huh, I wasn't aware of
pd.SparseArray
. I meant pd.SparseDataFrame, but as of 1.0.0 that no longer exists since the API for sparse stuff has totally changed. Do you think we should support Pandas < 1.0.0?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I discovered sparsity in pandas this morning. What I am currently doing is to assume someone is working with the latest pandas API. If they are not, then this will still work but there will be some performance penalty. We can tackle that in the future if it would be useful.
Currently (will push soon), there's native handling of
pd.Categorical
andpd.SparseArray
which are mapped tomx.CategoricalMatrix
andmx.SparseMatrix
, respectively. All the other numerical columns are converted to either SparseMatrix or DenseMatrix depending on data density.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't bend over backwards to support Pandas < 1.0. There were also substantial changes in the categorical type with 1.0, so we may also face some issues there. Still, we probably shouldn't require Pandas >= 1.0 in general. I think it's okay to say you can only use
from_pandas()
if you have>=1.0