print scipy sparse matrix without skipping lines

Viewed 116

I want to print all non zeros values of scipy sparse matrix, but it's print only head and tail of the values.

I have tried to use numpy as well as scipy set_printoptions, but it's not make any changes.

For example:

np.set_printoptions(threshold=np.inf, linewidth=np.inf, precision=5)

print(S_0)

When S_0 is a sparse matrix 4096 by 4096.

It's print:

  (0, 1)    0.05473236104737437
  (0, 63)   0.21776763895262571
  (0, 64)   0.11669233874304108
  (0, 65)   0.07099307125265637
  (0, 127)  0.16239160623342577
  (0, 4032) 0.15580766125695897
  (0, 4033) 0.03847165084209235
  (0, 4095) 0.27314367167182557
  (0, 0)    -0.09000000000000008
  (1, 1)    1.0
  (2, 1)    0.03201451256475778
  (2, 3)    0.2404854874352423
  (2, 65)   0.03313570631258726
  (2, 66)   0.2433307472199537
  (2, 67)   0.45352578812732014
  (2, 4033) 0.03089331881692829
  (2, 4034) 0.029169252780046343
  (2, 4035) 0.027445186743164394
  (2, 2)    -0.09000000000000008
  (3, 3)    1.0
  (4, 3)    0.1780272480832952
  (4, 5)    0.09447275191670479
  (4, 67)   0.09857505006040893
  (4, 68)   0.10065942747216487
  (4, 69)   0.10274380488392083
  : :
  (4071, 4071)  1.0
  (4072, 4072)  1.0
  (4073, 4073)  1.0
  (4074, 4074)  1.0
  (4075, 4075)  1.0
  (4076, 4076)  1.0
  (4077, 4077)  1.0
  (4078, 4078)  1.0
  (4079, 4079)  1.0
  (4080, 4080)  1.0
  (4081, 4081)  1.0
  (4082, 4082)  1.0
  (4083, 4083)  1.0
  (4084, 4084)  1.0
  (4085, 4085)  1.0
  (4086, 4086)  1.0
  (4087, 4087)  1.0
  (4088, 4088)  1.0
  (4089, 4089)  1.0
  (4090, 4090)  1.0
  (4091, 4091)  1.0
  (4092, 4092)  1.0
  (4093, 4093)  1.0
  (4094, 4094)  1.0
  (4095, 4095)  1.0

I want it to print all the values

Any ideas?

1 Answers

Had the same problem and setting numpy print options did not work. The following worked for me. (tested for scipy version 1.5.3 and 1.8.1)

import numpy as np
# ...
# let's say your S_0 is your sparse matrix, then this helps:
S0.maxprint = np.inf
print(S0) # now prints all entries

According to the docs there is a function getmaxprint(). Looking up the source code it just returns the attribute maxprint of the class, which by default is 50. So overwriting it with infinity will result in no limit for printing. Also you can provide it in the constructor of your sparse matrix. You can also see how the string for printing is created when looking at the function __str__ of the class spmatrix which is the base class of all sparse matrices of scipy (link to code)

Related