Created
March 11, 2013 22:36
-
-
Save larsmans/5138534 to your computer and use it in GitHub Desktop.
Columnwise maximum of scipy.sparse.csc_matrix, in Cython
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
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