Skip to content

Instantly share code, notes, and snippets.

@jni
Created July 31, 2013 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jni/6120922 to your computer and use it in GitHub Desktop.
Save jni/6120922 to your computer and use it in GitHub Desktop.
Failure case for sparse max() function
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