-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG+1] Scaling a sparse matrix along axis 0 should accept a csc by default #5791
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 |
---|---|---|
|
@@ -103,7 +103,7 @@ def scale(X, axis=0, with_mean=True, with_std=True, copy=True): | |
copy : boolean, optional, default True | ||
set to False to perform inplace row normalization and avoid a | ||
copy (if the input is already a numpy array or a scipy.sparse | ||
CSR matrix and if axis is 1). | ||
CSC matrix and if axis is 1). | ||
|
||
Notes | ||
----- | ||
|
@@ -113,18 +113,18 @@ def scale(X, axis=0, with_mean=True, with_std=True, copy=True): | |
|
||
Instead the caller is expected to either set explicitly | ||
`with_mean=False` (in that case, only variance scaling will be | ||
performed on the features of the CSR matrix) or to call `X.toarray()` | ||
performed on the features of the CSC matrix) or to call `X.toarray()` | ||
if he/she expects the materialized dense array to fit in memory. | ||
|
||
To avoid memory copy the caller should pass a CSR matrix. | ||
To avoid memory copy the caller should pass a CSC matrix. | ||
|
||
See also | ||
-------- | ||
:class:`sklearn.preprocessing.StandardScaler` to perform centering and | ||
8000 td> | scaling using the ``Transformer`` API (e.g. as part of a preprocessing | |
:class:`sklearn.pipeline.Pipeline`) | ||
""" | ||
X = check_array(X, accept_sparse='csr', copy=copy, ensure_2d=False, | ||
X = check_array(X, accept_sparse='csc', copy=copy, ensure_2d=False, | ||
warn_on_dtype=True, estimator='the scale function', | ||
dtype=FLOAT_DTYPES) | ||
if sparse.issparse(X): | ||
|
@@ -135,11 +135,6 @@ def scale(X, axis=0, with_mean=True, with_std=True, copy=True): | |
if axis != 0: | ||
raise ValueError("Can only scale sparse matrix on axis=0, " | ||
" got axis=%d" % axis) | ||
if not sparse.isspmatrix_csr(X): | ||
X = X.tocsr() | ||
copy = False | ||
if copy: | ||
X = X.copy() | ||
if with_std: | ||
_, var = mean_variance_axis(X, axis=0) | ||
var = _handle_zeros_in_scale(var, copy=False) | ||
|
@@ -150,8 +145,6 @@ def scale(X, axis=0, with_mean=True, with_std=True, copy=True): | |
mean_ = np.mean(X, axis) | ||
if with_std: | ||
scale_ = np.std(X, axis) | ||
if copy: | ||
X = X.copy() | ||
# Xr is a view on the original array that enables easy use of | ||
# broadcasting on the axis in which we are interested in | ||
Xr = np.rollaxis(X, axis) | ||
93AE
|
This file contains hidden or 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
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.
Why not
['csc, 'csr']
?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 did not write the initial code, but I think the idea is to convert it into a format where the column operations are sufficiently faster. I just maintained that convention because converting it to csr would be non-intuitive.
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.
Are you afraid that this will not be backward compatible?
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.
How much faster is converting to csc and multipying compared to not converting?
['csc', 'csr']
will not convert to CSR but will let CSR pass.