Created
July 31, 2013 10:17
-
-
Save jni/6120922 to your computer and use it in GitHub Desktop.
Failure case for sparse max() function
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
In [28]: x = np.zeros((3, 3), float) | |
In [29]: x[0, 0] = -3.2 | |
In [30]: x[2, 1] = -2.7 | |
In [31]: s = sparse.csr_matrix(x) | |
In [32]: s.data | |
Out[32]: array([-3.2, -2.7]) | |
In [33]: cpaste | |
Pasting code; enter '--' alone on the line to stop or use Ctrl-D. | |
:def sparse_max_row(csr_mat): | |
: ret = np.zeros(csr_mat.shape[0]) | |
: ret[np.diff(csr_mat.indptr) != 0] = np.maximum.reduceat(csr_mat.data,csr_mat.indptr[:-1][np.diff(csr_mat.indptr)>0]) | |
: return ret | |
: | |
:-- | |
In [34]: sparse_max_row(s) | |
Out[34]: array([-3.2, 0. , -2.7]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment