1

I'm trying to figure out how to efficiently sum .csr_matrix sparse matrices stored in a list. Here's the issue:

List_=list_of_sparse matrices
I tried the following
result=np.sum(List_)

But I a getting error!

How can I do it? any help will be much appreciated

Ammar Aslam
  • 560
  • 2
  • 16
TinaTz
  • 311
  • 3
  • 16
  • If you want real help, provide an actual example (e.g. [mcve]). Also show the error with traceback!!! That said, sparse matrices are not designed for efficient addition. Math that doesn't change the sparsity is ok, since it can focus on the `data` attribute. Matrix multiplication is also pretty good. But things like sum require figuring out where nonzeros match etc. – hpaulj Nov 25 '21 at 08:36
  • 1
    One possibility is to convert all the matrices to `coo` format, then concatenate their attributes (rows, cols, data). Make a new `coo` with these, and let the conversion to `csr` perform the addition. – hpaulj Nov 25 '21 at 08:37

2 Answers2

2

You can loop the list:

import numpy as np

List_ = [
    np.matrix([[0,0,0],[0,1,0],[0,0,0]]),
    np.matrix([[0,1,0],[0,0,0],[0,0,0]]),
    np.matrix([[0,0,0],[0,0,0],[0,1,0]]),
]

sum_in = np.matrix(
    [[0,0,0],[0,0,0],[0,0,0]]
)

for mat in List_:
    sum_in += mat

print(sum_in)
Ammar Aslam
  • 560
  • 2
  • 16
  • Thank @Ammar, but how can I create sum_in for sparse matrix? – TinaTz Nov 25 '21 at 07:43
  • 1
    You might have to convert it to a full matrix beforehand or look into the scipy package https://docs.scipy.org/doc/scipy/reference/sparse.html https://stackoverflow.com/questions/36969886/using-a-sparse-matrix-versus-numpy-array – Ammar Aslam Nov 25 '21 at 07:47
2

For example you have a list of sparse matrix like this:

from scipy.sparse import csr_matrix
import numpy as np
np.random.seed(111)

List_ = [csr_matrix(np.random.binomial(1,0.5,(2,2))) for i in range(3)]

If your array is huge, you should try not to convert it into dense (memory issues), there is already a addition method, so all you need to do is reduce:

from functools import reduce
result = reduce(lambda x,y:x+y,List_)
result.toarray()
array([[1, 0],
       [1, 1]])

As mentioned by @AmmarAslam, you can convert it to dense and sum:

[i.toarray() for i in List_]
[array([[1, 0],
    [0, 1]]),
 array([[0, 0],
        [0, 0]]),
 array([[0, 0],
        [1, 0]])]

Summing this up is a matter of:

np.add.reduce([i.toarray() for i in List_])

array([[1, 0],
       [1, 1]])
StupidWolf
  • 45,075
  • 17
  • 40
  • 72