Printing FULL contents of numpy array

43,565

Of course, you can change the print threshold of the array as answered elsewhere with:

np.set_printoptions(threshold=np.nan)

But depending on what you're trying to look at, there's probably a better way to do that. For example, if your array truly is mostly zeros as you've shown, and you want to check whether it has values that are nonzero, you might look at things like:

import numpy as np
import matplotlib.pyplot as plt

In [1]: a = np.zeros((100,100))

In [2]: a
Out[2]: 
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.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])

Change some values:

In [3]: a[4:19,5:20] = 1

And it still looks the same:

In [4]: a
Out[4]: 
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.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])

Check some things that don't require manually looking at all values:

In [5]: a.sum()
Out[5]: 225.0

In [6]: a.mean()
Out[6]: 0.022499999999999999

Or plot it:

In [7]: plt.imshow(a)
Out[7]: <matplotlib.image.AxesImage at 0x1043d4b50>

Or save to a file:

In [11]: np.savetxt('file.txt', a)

array

Share:
43,565
user2275931
Author by

user2275931

Updated on April 13, 2020

Comments

  • user2275931
    user2275931 about 4 years

    I am working with image processing in python and I want to output a variable, right now the variable b is a numpy array with shape (200,200). When I do print b all I see is:

    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.,  0.,  0., ...,  0.,  0.,  0.],
           [ 0.,  0.,  0., ...,  0.,  0.,  0.]])
    

    How do I print out the full contents of this array, write it to a file or something simple so I can just look at the contents in full?