Aufwind
Aufwind

Reputation: 26258

How to select the maximum value over all rows from a list of scipy.sparse.arrays?

Consider a list of n scipy.sparse.arrays with entries of type float. I am using the in Compressed Sparse Row format structure.

my_list = [sparse_array_1, sparse_array_2, ... , sparse_array_n]

Each sparse_array_i has the same length.

What I want to generate is a list of maximum per row values. So this example

[array[0,  array[4,       array[88,
       3,        2,             287,
       99,        1234,         0,
       3],       0],            77]

would result in

[88, 287, 1324, 77]

Is this possible in a pythonic way?

Upvotes: 0

Views: 273

Answers (2)

user1334060
user1334060

Reputation: 11

Here's the answer for two sparse matrices: just repeat this n-1 times.

import numpy as np
def spmax(X,Y):
   # X,Y two csr sparse matrices
   sX = X.copy(); sX.data[:] = 1
   sY = Y.copy(); sY.data[:] = 1
   sXY = sX+sY; sXY.data[:] = 1
   X = X+sXY; X.data = X.data-1
   Y = Y+sXY; Y.data = Y.data-1
   maxXY = X.copy()
   maxXY.data = np.amax(np.c_[X.data,Y.data],axis=1)
   return maxXY

This is pretty slow though. Hopefully, they'll implement this in scipy.sparse at some point. This is a pretty basic operation.

Upvotes: 1

Jesse
Jesse

Reputation: 5074

I'm not familiar with scipy sparse arrays, but if they behave like other python iterables then a combination of map and zip will achieve what you want:

>>> arr
[[0, 3, 99, 3], [4, 2, 1234, 0], [88, 287, 0, 77]]
>>> zip(*arr)
[(0, 4, 88), (3, 2, 287), (99, 1234, 0), (3, 0, 77)]
>>> map(max, zip(*arr))
[88, 287, 1234, 77]

Upvotes: 3

Related Questions