Access to items in coo_matrix

This is a very simple question. For sparse SciPy matrices such as coo_matrix, how can I access individual elements?

Draw an analogy with your own library of linear algebras. Access to element (i, j) can be obtained using coeffRef as follows:

myMatrix.coeffRef(i,j) 
+6
source share
2 answers

From the docs for coo_matrix:

  | Intended Usage | - COO is a fast format for constructing sparse matrices | - Once a matrix has been constructed, convert to CSR or | CSC format for fast arithmetic and matrix vector operations | - By default when converting to CSR or CSC format, duplicate (i,j) | entries will be summed together. This facilitates efficient | construction of finite element matrices and the like. (see example) 

Indeed, csr_matrix supports indexing as expected:

 >>> from scipy.sparse import coo_matrix >>> m = coo_matrix([[1, 2, 3], [4, 5, 6]]) >>> m1 = m.tocsr() >>> m1[1, 2] 6 >>> m1 <2x3 sparse matrix of type '<type 'numpy.int64'>' with 6 stored elements in Compressed Sparse Row format> 

(The way I found the above quote from the docs was >>> help(m) , which is equivalent to online docs ).

+12
source

To expand the conversion of coo matrix to csr index for indexing, here are some timings for a small sparse matrix

Make matrix

 In [158]: M=sparse.coo_matrix([[0,1,2,0,0],[0,0,0,1,0],[0,1,0,0,0]]) In [159]: timeit M[1,2] TypeError: 'coo_matrix' object is not subscriptable In [160]: timeit M.tocsc()[1,2] 1000 loops, best of 3: 375 µs per loop In [161]: timeit M.tocsr()[1,2] 1000 loops, best of 3: 241 µs per loop In [162]: timeit M.todok()[1,2] 10000 loops, best of 3: 65.8 µs per loop In [163]: timeit M.tolil()[1,2] 1000 loops, best of 3: 270 µs per loop 

Apparently, to select one dok element, is fast (counting the conversion time). This format is actually a dictionary, which, of course, has quick access to the element.

But if you often refer to entire rows or entire columns or iterate over rows or columns, you need to read the documents more carefully and possibly run your own time tests of typical arrays.

If you set values, and not just read them, the timings and even the implementation may differ. You will get a performance warning if you try to change element 0 the csr or csc .

+5
source

Source: https://habr.com/ru/post/986958/


All Articles