[SciPy-user] fast max() on sparse matrices

Bill Baxter wbaxter@gmail....
Sun Jan 4 20:48:11 CST 2009


On Mon, Jan 5, 2009 at 11:37 AM, Peter Skomoroch
<peter.skomoroch@gmail.com> wrote:
> Does anyone have suggestions on a fast max() function for sparse matrices
> (COO, CSC, or CSR format)?
>
> I was thinking of slicing CSC or CSR matrices, and iterating through the
> columns, but I suspect any loop based approach will be slow.
>
> def sparse_amax(V):
>     """Returns the max of a sparse CSR matrix V with shape (m,n)
>     m = number of examples (# columns),
>     n = dimensionality of examples (# rows) """
>     n,m = V.shape
>     # if type is CSR, slice by rows
>     maxvals = []
>     for row in xrange(n):
>         #find max of row
>         maxvals.append(max(array(V[row,:].todense())[0]))
>     Vmax = max(maxvals)
>     return Vmax

The CSC and CSR formats both internally store a dense array of all the
non-zero values.
I'm not sure how the Python interface looks like in SciPy's versions,
but if there's a way to get at that values array, then you can just do
the max of that.  (But don't forget the corner case of an unset
implicit zero value being the max).

--bb


More information about the SciPy-user mailing list