Search code examples
pythonpython-2.7numpyscipysparse-matrix

Printing complete coo_matrix


I tried to instantiate a coo_matrix using another matrix. When I tried to print the coo_matrix, the output is:

(1, 9)  1.0
(1, 10) 1.0
(1, 11) 1.0
(1, 25) 1.0
(1, 47) 1.0
(2, 1)  1.0
(2, 7)  1.0
(2, 11) 3.0
(2, 12) 1.0
(2, 13) 1.0
(2, 15) 2.0
(2, 19) 1.0
(2, 42) 1.0
(3, 0)  1.0
(4, 20) 1.0
(4, 22) 1.0
(4, 24) 1.0
:   :
(45, 0) 1.0
(45, 7) 1.0
(45, 14)    2.0
(45, 20)    1.0
(45, 26)    1.0
(45, 38)    1.0
(45, 40)    1.0
(46, 11)    1.0
(46, 19)    1.0
(46, 36)    1.0
(46, 41)    1.0
(46, 47)    1.0

How do I print the complete coo_matrix? I tried using set_printoptions(threshold = 'nan') but it doesn't work on this.


Solution

  • You can convert your sparse matrix to a dense matrix using .todense() method:

    print(my_coo_matrix.todense())
    

    Edit: Your question sounded like you want to print the zero valued elements too, however if you only want to print nonzero elements, you can iterate the matrix manually:

    for row, col, value in zip(my_coo_matrix.row, my_coo_matrix.col, my_coo_matrix.data):
        print "({0}, {1}) {2}".format(row, col, value)