11

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?

askewchan
  • 45,161
  • 17
  • 118
  • 134
user2275931
  • 111
  • 1
  • 1
  • 3

2 Answers2

16

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

Community
  • 1
  • 1
askewchan
  • 45,161
  • 17
  • 118
  • 134
  • Awesome, the np.savetxt command will definitely come in handy. Thank you.I tried using .write but it ended up just saving the truncated version shown above. – user2275931 Apr 12 '13 at 23:11
  • You're welcome @user2275931, and Welcome to [SO]! If this answers your question, you can click the checkmark by it to 'accept' the answer. – askewchan Apr 12 '13 at 23:12
  • @user2275931 Great, also note that [`np.savetxt`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html) takes all sorts of formatting options as well. – askewchan Apr 12 '13 at 23:15
0
to_print = "\n".join([", ".join(row) for row in b])
print (to_print) #console

f = open("path-to-file", "w")
f.write(to_print) #to file

In case it's numpy array: Print the full numpy array

Community
  • 1
  • 1
J0HN
  • 26,063
  • 5
  • 54
  • 85