Python 打印 numpy 数组的全部内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15980405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:31:02  来源:igfitidea点击:

Printing FULL contents of numpy array

pythonprintingnumpy

提问by user2275931

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

我正在使用 python 进行图像处理,我想输出一个变量,现在该变量b是一个形状为 numpy 的数组(200,200)。当我做的时候,print b我看到的是:

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?

我如何打印出这个数组的全部内容,将它写入一个文件或一些简单的东西,以便我可以完整地查看内容?

回答by J0HN

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

如果是 numpy 数组:打印完整的 numpy 数组

回答by askewchan

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

当然,您可以更改数组的打印阈值,如其他地方回答

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

大批