I would like to print numpy
array
with indentation for debugging.
Say I have an array
a = numpy.array([[1,2,3,4], [5,6,7,8]])
, then simple print(a)
will give
[[ 63 903 942 952]
[185 332 511 893]]
Now if I put in \t
in print("\t" + str(a))
, then I get
[[ 63 903 942 952]
[185 332 511 893]]
while I want to have
[[ 63 903 942 952]
[185 332 511 893]]
To print a NumPy array without enclosing square brackets, the most Pythonic way is to unpack all array values into the print() function and use the sep=', ' argument to separate the array elements with a comma and a space.
In Python, we use the set_printoptions() function to set the way floating-point number(s), array(s), and other NumPy objects are displayed.
This should do it :
print('\t' + str(a).replace('\n', '\n\t'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With