12

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?

2
  • First show output of print type(b) Commented Apr 12, 2013 at 20:46
  • wait a second, is it numpy array? Commented Apr 12, 2013 at 20:50

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
You're welcome @user2275931, and Welcome to Stack Overflow! If this answers your question, you can click the checkmark by it to 'accept' the answer.
@user2275931 Great, also note that np.savetxt takes all sorts of formatting options as well.
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

3 Comments

Yes it is numpy.ndarray
Okay that helps a lot, but what is the structure of this output? each [ ] vector is a single row right?
Yes (at least it should be :)). Try with smaller array with clear elements pattern, e.g. [[1,2,3], [4,5,6]] to understand the structure.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.