-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG+1] Add 'axis' argument to sparsefuncs.mean_variance_axis #3622
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
Closed
Closed
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
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
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
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
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
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
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
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 |
---|---|---|
|
@@ -6,8 +6,8 @@ | |
import numpy as np | ||
|
||
from .fixes import sparse_min_max | ||
from .sparsefuncs_fast import (csr_mean_variance_axis0, | ||
csc_mean_variance_axis0) | ||
from .sparsefuncs_fast import csr_mean_variance_axis0 as _csr_mean_var_axis0 | ||
from .sparsefuncs_fast import csc_mean_variance_axis0 as _csc_mean_var_axis0 | ||
|
||
|
||
def _raise_typeerror(X): | ||
|
@@ -53,14 +53,17 @@ def inplace_csr_row_scale(X, scale): | |
X.data *= np.repeat(scale, np.diff(X.indptr)) | ||
|
||
|
||
def mean_variance_axis0(X): | ||
def mean_variance_axis(X, axis): | ||
"""Compute mean and variance along axis 0 on a CSR or CSC matrix | ||
|
||
Parameters | ||
---------- | ||
X: CSR or CSC sparse matrix, shape (n_samples, n_features) | ||
Input data. | ||
|
||
axis: int (either 0 or 1) | ||
Axis along which the axis should be computed. | ||
|
||
Returns | ||
------- | ||
|
||
|
@@ -71,10 +74,20 @@ def mean_variance_axis0(X): | |
Feature-wise variances | ||
|
||
""" | ||
if axis not in (0, 1): | ||
raise ValueError( | ||
"Unknown axis value: %d. Use 0 for rows, or 1 for columns" % axis) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be better if axis was the axis given by the user. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
if isinstance(X, sp.csr_matrix): | ||
return csr_mean_variance_axis0(X) | ||
if axis == 0: | ||
return _csr_mean_var_axis0(X) | ||
else: | ||
return _csc_mean_var_axis0(X.T) | ||
elif isinstance(X, sp.csc_matrix): | ||
return csc_mean_variance_axis0(X) | ||
if axis == 0: | ||
return _csc_mean_var_axis0(X) | ||
else: | ||
return _csr_mean_var_axis0(X.T) | ||
else: | ||
_raise_typeerror(X) | ||
|
||
|
@@ -258,13 +271,16 @@ def inplace_swap_column(X, m, n): | |
|
||
|
||
def min_max_axis(X, axis): | ||
"""Compute minimum and maximum along axis 0 on a CSR or CSC matrix | ||
"""Compute minimum and maximum along an axis on a CSR or CSC matrix | ||
|
||
Parameters | ||
---------- | ||
X : CSR or CSC sparse matrix, shape (n_samples, n_features) | ||
Input data. | ||
|
||
axis: int (either 0 or 1) | ||
Axis along which the axis should be computed. | ||
|
||
Returns | ||
------- | ||
|
||
|
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.
Apparently, you also accept -1 and -2.
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.
True, out of consistency with other methods in sklearn (and scipy in general) that handle the axis argument this way as well (e.g.
count_nonzero
in the same file), but those function don't document that usage, either. I assumed this is an sklearn convention.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.
e.g. see also https://github.com/scipy/scipy/blob/master/scipy/sparse/compressed.py which uses the same convention thoughout, but never documents it.
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.
Whereas the
numpy.matrix.std
has a docstring that says: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.
grepping through the numpy and scipy codebases, it seems like the most common way is to describe this as "axis : int" without specifying which values are allowed (which makes sense for numpy given that an ndarray can have any number of axis), while the
scipy.sparse
module explicitly lists 0 and 1 as valid arguments (never -1 and -2, although the functions in questions do accept those values as well). Personally I think the way I documented it makes sense, as it's consistent with scipy.sparseThere 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.
count_nonzero
is a backport from NumPy. We don't generally accept funny axes, since data is assumed to be 2-d almost everywhere.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.
So what do you suggest would be the right thing to do? Remove -2/-1 as accepted values?
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.
Yes, I'd get rid of those. They're unlikely to be more useful than confusing.
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.
Perhaps more to the point, unlike scipy.sparse, utils here are not public.
On 3 September 2014 04:21, Lars Buitinck [email protected] wrote: