如何打印完整的NumPy数组?

时间:2021-07-25 03:15:14

When I print a numpy array, I get a truncated representation, but I want the full array.

当我打印一个numpy数组时,我得到一个被截断的表示,但是我想要完整的数组。

Is there any way to do this?

有什么办法吗?

Examples:

例子:

>>> numpy.arange(10000)
array([   0,    1,    2, ..., 9997, 9998, 9999])
>>> numpy.arange(10000).reshape(250,40)
array([[   0,    1,    2, ...,   37,   38,   39],
       [  40,   41,   42, ...,   77,   78,   79],
       [  80,   81,   82, ...,  117,  118,  119],
       ..., 
       [9880, 9881, 9882, ..., 9917, 9918, 9919],
       [9920, 9921, 9922, ..., 9957, 9958, 9959],
       [9960, 9961, 9962, ..., 9997, 9998, 9999]])

12 个解决方案

#1


333  

To clarify on Reed's reply

澄清里德的回答

import numpy
numpy.set_printoptions(threshold=numpy.nan)

Note that the reply as given above works with an initial from numpy import *, which is not advisable. This also works for me:

注意,上面给出的回复使用来自numpy import *的初始值,这是不可取的。这也适用于我:

numpy.set_printoptions(threshold='nan')

For full documentation, see http://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html.

有关完整的文档,请参见http://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html。

#2


137  

import numpy as np
np.set_printoptions(threshold=np.inf)

I suggest using np.inf instead of np.nan which is suggested by others. They both work for your purpose, but by setting the threshold to "infinity" it is obvious to everybody reading your code what you mean. Having a threshold of "not a number" seems a little vague to me.

我建议使用np。np的正相反。南是别人建议的。它们都是为您的目的而工作的,但是通过将阈值设置为“无穷大”,每个阅读您的代码的人都可以清楚地看到您的意思。在我看来,“不是一个数字”的阈值有点模糊。

#3


35  

This sounds like you're using numpy.

听起来你在用numpy。

If that's the case, you can add:

如果是这样,你可以加上:

import numpy as np
np.set_printoptions(threshold='nan')

That will disable the corner printing. For more information, see this NumPy Tutorial.

这将禁用角打印。有关更多信息,请参见这个NumPy教程。

#4


35  

The previous answers are the correct ones, but as a weaker alternative you can transform into a list:

前面的答案是正确的,但作为一种较弱的选择,你可以将其转化为一个列表:

>>> numpy.arange(100).reshape(25,4).tolist()

[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21,
22, 23], [24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35], [36, 37, 38, 39], [40, 41,
42, 43], [44, 45, 46, 47], [48, 49, 50, 51], [52, 53, 54, 55], [56, 57, 58, 59], [60, 61,
62, 63], [64, 65, 66, 67], [68, 69, 70, 71], [72, 73, 74, 75], [76, 77, 78, 79], [80, 81,
82, 83], [84, 85, 86, 87], [88, 89, 90, 91], [92, 93, 94, 95], [96, 97, 98, 99]]

#5


28  

Here is a one-off way to do this, which is useful if you don't want to change your default settings:

这里有一个一次性的方法,如果你不想改变你的默认设置,这是很有用的:

def fullprint(*args, **kwargs):
  from pprint import pprint
  import numpy
  opt = numpy.get_printoptions()
  numpy.set_printoptions(threshold='nan')
  pprint(*args, **kwargs)
  numpy.set_printoptions(**opt)

#6


18  

Using a context manager as Paul Price sugggested

正如保罗•普莱斯建议的那样,聘请一位上下文经理

import numpy as np


class fullprint:
    'context manager for printing full numpy arrays'

    def __init__(self, **kwargs):
        if 'threshold' not in kwargs:
            kwargs['threshold'] = np.nan
        self.opt = kwargs

    def __enter__(self):
        self._opt = np.get_printoptions()
        np.set_printoptions(**self.opt)

    def __exit__(self, type, value, traceback):
        np.set_printoptions(**self._opt)

a = np.arange(1001)

with fullprint():
    print(a)

print(a)

with fullprint(threshold=None, edgeitems=10):
    print(a)

#7


9  

numpy.savetxt

numpy.savetxt

numpy.savetxt(sys.stdout, numpy.arange(10000))

or if you need a string:

或者如果你需要一个字符串:

import StringIO
sio = StringIO.StringIO()
numpy.savetxt(sio, numpy.arange(10000))
s = sio.getvalue()
print s

The default output format is:

默认输出格式为:

0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
...

and it can be configured with further arguments.

它可以用进一步的参数进行配置。

Tested on Python 2.7.12, numpy 1.11.1.

在Python 2.7.12、numpy 1.11.1上测试。

#8


7  

This is a slight modification (removed the option to pass additional arguments to set_printoptions)of neoks answer.

这是对neoks答案的一个微小修改(删除了将附加参数传递给set_printoptions的选项)。

It shows how you can use contextlib.contextmanager to easily create such a contextmanager with fewer lines of code:

它展示了如何使用contextlib。用更少的代码行轻松创建这样的contextmanager:

import numpy as np
from contextlib import contextmanager

@contextmanager
def show_complete_array():
    oldoptions = np.get_printoptions()
    np.set_printoptions(threshold=np.inf)
    try:
        yield
    finally:
        np.set_printoptions(**oldoptions)

In your code it can be used like this:

在您的代码中可以这样使用:

a = np.arange(1001)

print(a)      # shows the truncated array

with show_complete_array():
    print(a)  # shows the complete array

print(a)      # shows the truncated array (again)

#9


6  

For these who like to import as np:

对于那些喜欢将其导入为np的人:

import numpy as np
np.set_printoptions(threshold=np.nan)

Will also work

也会工作

#10


1  

Suppose you have a numpy array

假设有一个numpy数组

 arr = numpy.arange(10000).reshape(250,40)

If you want to print the full array in a one-off way (without toggling np.set_printoptions), but want something simpler (less code) than the context manager, just do

如果您想以一次性的方式打印完整的数组(不需要切换到np.set_printoptions),但是想要比上下文管理器更简单的东西(更少的代码),只需这样做

for row in arr:
     print row 

#11


0  

If an array is too large to be printed, NumPy automatically skips the central part of the array and only prints the corners: To disable this behaviour and force NumPy to print the entire array, you can change the printing options using set_printoptions.

如果数组太大,无法打印,NumPy会自动跳过数组的中心部分,只打印边角:要禁用这种行为并强制NumPy打印整个数组,可以使用set_printoptions更改打印选项。

>>> np.set_printoptions(threshold='nan')

or

>>> np.set_printoptions(edgeitems=3,infstr='inf',
... linewidth=75, nanstr='nan', precision=8,
... suppress=False, threshold=1000, formatter=None)

You can also refer to the numpy documentation numpy documentation for "or part" for more help.

您还可以参考numpy文档中“或部分”的numpy文档来获得更多帮助。

#12


-4  

its just like python's range, use np.range(10001) welcome!!

就像python的range,使用np.range(10001)欢迎!

#1


333  

To clarify on Reed's reply

澄清里德的回答

import numpy
numpy.set_printoptions(threshold=numpy.nan)

Note that the reply as given above works with an initial from numpy import *, which is not advisable. This also works for me:

注意,上面给出的回复使用来自numpy import *的初始值,这是不可取的。这也适用于我:

numpy.set_printoptions(threshold='nan')

For full documentation, see http://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html.

有关完整的文档,请参见http://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html。

#2


137  

import numpy as np
np.set_printoptions(threshold=np.inf)

I suggest using np.inf instead of np.nan which is suggested by others. They both work for your purpose, but by setting the threshold to "infinity" it is obvious to everybody reading your code what you mean. Having a threshold of "not a number" seems a little vague to me.

我建议使用np。np的正相反。南是别人建议的。它们都是为您的目的而工作的,但是通过将阈值设置为“无穷大”,每个阅读您的代码的人都可以清楚地看到您的意思。在我看来,“不是一个数字”的阈值有点模糊。

#3


35  

This sounds like you're using numpy.

听起来你在用numpy。

If that's the case, you can add:

如果是这样,你可以加上:

import numpy as np
np.set_printoptions(threshold='nan')

That will disable the corner printing. For more information, see this NumPy Tutorial.

这将禁用角打印。有关更多信息,请参见这个NumPy教程。

#4


35  

The previous answers are the correct ones, but as a weaker alternative you can transform into a list:

前面的答案是正确的,但作为一种较弱的选择,你可以将其转化为一个列表:

>>> numpy.arange(100).reshape(25,4).tolist()

[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21,
22, 23], [24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35], [36, 37, 38, 39], [40, 41,
42, 43], [44, 45, 46, 47], [48, 49, 50, 51], [52, 53, 54, 55], [56, 57, 58, 59], [60, 61,
62, 63], [64, 65, 66, 67], [68, 69, 70, 71], [72, 73, 74, 75], [76, 77, 78, 79], [80, 81,
82, 83], [84, 85, 86, 87], [88, 89, 90, 91], [92, 93, 94, 95], [96, 97, 98, 99]]

#5


28  

Here is a one-off way to do this, which is useful if you don't want to change your default settings:

这里有一个一次性的方法,如果你不想改变你的默认设置,这是很有用的:

def fullprint(*args, **kwargs):
  from pprint import pprint
  import numpy
  opt = numpy.get_printoptions()
  numpy.set_printoptions(threshold='nan')
  pprint(*args, **kwargs)
  numpy.set_printoptions(**opt)

#6


18  

Using a context manager as Paul Price sugggested

正如保罗•普莱斯建议的那样,聘请一位上下文经理

import numpy as np


class fullprint:
    'context manager for printing full numpy arrays'

    def __init__(self, **kwargs):
        if 'threshold' not in kwargs:
            kwargs['threshold'] = np.nan
        self.opt = kwargs

    def __enter__(self):
        self._opt = np.get_printoptions()
        np.set_printoptions(**self.opt)

    def __exit__(self, type, value, traceback):
        np.set_printoptions(**self._opt)

a = np.arange(1001)

with fullprint():
    print(a)

print(a)

with fullprint(threshold=None, edgeitems=10):
    print(a)

#7


9  

numpy.savetxt

numpy.savetxt

numpy.savetxt(sys.stdout, numpy.arange(10000))

or if you need a string:

或者如果你需要一个字符串:

import StringIO
sio = StringIO.StringIO()
numpy.savetxt(sio, numpy.arange(10000))
s = sio.getvalue()
print s

The default output format is:

默认输出格式为:

0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
...

and it can be configured with further arguments.

它可以用进一步的参数进行配置。

Tested on Python 2.7.12, numpy 1.11.1.

在Python 2.7.12、numpy 1.11.1上测试。

#8


7  

This is a slight modification (removed the option to pass additional arguments to set_printoptions)of neoks answer.

这是对neoks答案的一个微小修改(删除了将附加参数传递给set_printoptions的选项)。

It shows how you can use contextlib.contextmanager to easily create such a contextmanager with fewer lines of code:

它展示了如何使用contextlib。用更少的代码行轻松创建这样的contextmanager:

import numpy as np
from contextlib import contextmanager

@contextmanager
def show_complete_array():
    oldoptions = np.get_printoptions()
    np.set_printoptions(threshold=np.inf)
    try:
        yield
    finally:
        np.set_printoptions(**oldoptions)

In your code it can be used like this:

在您的代码中可以这样使用:

a = np.arange(1001)

print(a)      # shows the truncated array

with show_complete_array():
    print(a)  # shows the complete array

print(a)      # shows the truncated array (again)

#9


6  

For these who like to import as np:

对于那些喜欢将其导入为np的人:

import numpy as np
np.set_printoptions(threshold=np.nan)

Will also work

也会工作

#10


1  

Suppose you have a numpy array

假设有一个numpy数组

 arr = numpy.arange(10000).reshape(250,40)

If you want to print the full array in a one-off way (without toggling np.set_printoptions), but want something simpler (less code) than the context manager, just do

如果您想以一次性的方式打印完整的数组(不需要切换到np.set_printoptions),但是想要比上下文管理器更简单的东西(更少的代码),只需这样做

for row in arr:
     print row 

#11


0  

If an array is too large to be printed, NumPy automatically skips the central part of the array and only prints the corners: To disable this behaviour and force NumPy to print the entire array, you can change the printing options using set_printoptions.

如果数组太大,无法打印,NumPy会自动跳过数组的中心部分,只打印边角:要禁用这种行为并强制NumPy打印整个数组,可以使用set_printoptions更改打印选项。

>>> np.set_printoptions(threshold='nan')

or

>>> np.set_printoptions(edgeitems=3,infstr='inf',
... linewidth=75, nanstr='nan', precision=8,
... suppress=False, threshold=1000, formatter=None)

You can also refer to the numpy documentation numpy documentation for "or part" for more help.

您还可以参考numpy文档中“或部分”的numpy文档来获得更多帮助。

#12


-4  

its just like python's range, use np.range(10001) welcome!!

就像python的range,使用np.range(10001)欢迎!