Fields of a sparse matrix

In [5]: from scipy import sparse as spr 

In [6]: H= spr.dok_matrix((5,5))

In [7]: H.A
Out[7]: 
array([[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.]])

In [8]: H
Out[8]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
	with 0 stored elements in Dictionary Of Keys format>

It seems that the sparse matrix ‘H’ has a field named ‘A’, and the matrix is actually stored in this field.

Why is it named ‘A’? What other fields does ‘H’ have?

Hi,

if you type H., and wait about ~2 to 3 seconds, a drop-down menu of the matrix attributes will appear. From the following two sources:

  1. scipy.sparse.dok_matrix — SciPy v1.11.4 Manual

  2. and by typing help(scipy), for help on thie package, on the Python editor

there was no information stated (at least that I could find) describing this attribute. However, from the outcome after executing this command, it appears that the A stands for array, hence the reason why the array is displayed when executing the command of the name of the array followed by this attribute.

No, the matrix is not stored in this field. A is an attribute of H.

So, for example, if I created the following list:

>>> J = [4,5,6,7]  
>>> J
>>> [4,5,6,7]  # This is displayed after typing J

Notice that the same is not true for H above. If you type H, the following is displayed:

>>> H
<5x5 sparse matrix of type '<class 'numpy.float64'>'
	with 0 stored elements in Dictionary Of Keys format>

Thus, by how they designed this class object, in order to get the array to be displayed, the designers of this package have designed it such that you have to enter H.A, and not just H as in the case of the list J.

1 Like

Thanks a lot!

Is it possible to list all the attributes of the sparse matrix?

aha, after typing the tab, a drop down menu appears, and I saw dozens of attributes

yes, type the following:

dir(H)

and

dir(H.A)