How does this sparse matrix gets created?

Viewed 25

While inspecting the following notebook i got confused at how did he created the sparse matrix called M, i've tried to understand how coo_matrix works in this case but couldn't get it after looking for scipy docs on the function. In particular the code block is the following:

import scipy.sparse as sparse   
n = len(Y)
k = theta.shape[0]
data = [1]*n

M = sparse.coo_matrix((data, (Y, range(n))), shape=(k,n)).toarray()

from: https://www.kaggle.com/code/synnfusion/softmax-on-mnist-from-scratch/notebook

Y is a vector of labels (digits 0-9) from the mnist dataset.

Thanks.

1 Answers
In [90]: from scipy import sparse

Making the inputs:

In [91]: Y = np.array([0,3,2,1,5,5,3])    
In [92]: n=len(Y)    
In [93]: data = [1]*n

Making the sparse matrix:

In [94]: M = sparse.coo_matrix((data, (Y, range(n))), shape=(10,n))

Looking at it in various ways:

In [95]: M
Out[95]: 
<10x7 sparse matrix of type '<class 'numpy.int32'>'
    with 7 stored elements in COOrdinate format>

In [96]: print(M)
  (0, 0)    1
  (3, 1)    1
  (2, 2)    1
  (1, 3)    1
  (5, 4)    1
  (5, 5)    1
  (3, 6)    1

In [97]: M.toarray()
Out[97]: 
array([[1, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 1],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])

The matrix has 3 key attributes, which should match closely to the inputs:

In [98]: M.data
Out[98]: array([1, 1, 1, 1, 1, 1, 1])    
In [99]: M.row
Out[99]: array([0, 3, 2, 1, 5, 5, 3])    
In [100]: M.col
Out[100]: array([0, 1, 2, 3, 4, 5, 6], dtype=int32)

Given inputs like this there isn't much to creating the matrix - take inputs as is, or tweaking them a bit to make the first kind of array.

The work is in the methods, such as the toarray, which creates a dense array. How that sparse matrix maps on to that array should be obvious, though the code details are buried. Judging from some errors I've seen, I think it actually does: M.tocsr().toarray(). The csr format implements most of the sparse math, as well as indexing. The coo format is a basic one, most useful for initially creating the matrix.

Related