Skip to content

Instantly share code, notes, and snippets.

@larsmans
Created March 11, 2013 22:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsmans/5138534 to your computer and use it in GitHub Desktop.
Save larsmans/5138534 to your computer and use it in GitHub Desktop.
Columnwise maximum of scipy.sparse.csc_matrix, in Cython
cimport numpy as np
def csc_columnwise_max(np.ndarray[np.float64_t, ndim=1] data,
np.ndarray[int, ndim=1] indices,
np.ndarray[int, ndim=1] indptr,
np.ndarray[np.float64_t, ndim=1] out):
cdef double mx
cdef int n_features = indptr.shape[0] - 1
cdef int i, j
for i in range(n_features):
mx = 0
for j in range(indptr[i], indptr[i + 1]):
if data[j] < 0:
raise ValueError("negative value %r in column %d"
% (data[j], i))
mx = max(data[j], mx)
out[i] = mx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment