如何打印完整的 NumPy 数组而不截断?

发布于 2024-08-16 17:14:04 字数 514 浏览 13 评论 0 原文

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

>>> 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]])

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

>>> 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]])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

零崎曲识 2024-08-23 17:14:05

对此答案的补充,来自最大列数(用numpy.set_printoptions(threshold=numpy.nan)修复) ),显示的字符也有限制。在某些环境中,例如从 bash(而不是交互式会话)调用 python 时,可以通过设置参数 linewidth 来解决此问题,如下所示。

import numpy as np
np.set_printoptions(linewidth=2000)    # default = 75
Mat = np.arange(20000,20150).reshape(2,75)    # 150 elements (75 columns)
print(Mat)

在这种情况下,您的窗口应该限制换行的字符数。

对于那些使用 sublime text 并希望在输出窗口中查看结果的人,您应该将构建选项 "word_wrap": false 添加到 sublime-build 文件 [来源] 。

Complementary to this answer from the maximum number of columns (fixed with numpy.set_printoptions(threshold=numpy.nan)), there is also a limit of characters to be displayed. In some environments like when calling python from bash (rather than the interactive session), this can be fixed by setting the parameter linewidth as following.

import numpy as np
np.set_printoptions(linewidth=2000)    # default = 75
Mat = np.arange(20000,20150).reshape(2,75)    # 150 elements (75 columns)
print(Mat)

In this case, your window should limit the number of characters to wrap the line.

For those out there using sublime text and wanting to see results within the output window, you should add the build option "word_wrap": false to the sublime-build file [source] .

风向决定发型 2024-08-23 17:14:05

将其关闭并返回正常模式

np.set_printoptions(threshold=False)

To turn it off and return to the normal mode

np.set_printoptions(threshold=False)
┊风居住的梦幻卍 2024-08-23 17:14:05

自 NumPy 版本 1.16 起,有关更多详细信息,请参阅 GitHub 票证 12251

from sys import maxsize
from numpy import set_printoptions

set_printoptions(threshold=maxsize)

Since NumPy version 1.16, for more details see GitHub ticket 12251.

from sys import maxsize
from numpy import set_printoptions

set_printoptions(threshold=maxsize)
旧时模样 2024-08-23 17:14:05

假设你有一个 numpy 数组

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

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

for row in arr:
     print row 

Suppose you have a numpy array

 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

for row in arr:
     print row 
假装不在乎 2024-08-23 17:14:05

如果您使用的是 jupyter 笔记本,我发现这是针对一次性情况的最简单的解决方案。基本上将 numpy 数组转换为列表,然后转换为字符串,然后打印。这样做的好处是可以将逗号分隔符保留在数组中,而使用 numpyp.printoptions(threshold=np.inf) 则不会:

import numpy as np
print(str(np.arange(10000).reshape(250,40).tolist()))

If you're using a jupyter notebook, I found this to be the simplest solution for one off cases. Basically convert the numpy array to a list and then to a string and then print. This has the benefit of keeping the comma separators in the array, whereas using numpyp.printoptions(threshold=np.inf) does not:

import numpy as np
print(str(np.arange(10000).reshape(250,40).tolist()))
念﹏祤嫣 2024-08-23 17:14:05

这是最黑客的解决方案,它甚至可以像 numpy 一样很好地打印它:

import numpy as np

a = np.arange(10000).reshape(250,40)

b = [str(row) for row in a.tolist()]

print('\n'.join(b))

Out:

终端输出

This is the hackiest solution it even prints it nicely as numpy does:

import numpy as np

a = np.arange(10000).reshape(250,40)

b = [str(row) for row in a.tolist()]

print('\n'.join(b))

Out:

output in terminal

小忆控 2024-08-23 17:14:05

您可以使用 array2string 函数 - 文档

a = numpy.arange(10000).reshape(250,40)
print(numpy.array2string(a, threshold=numpy.nan, max_line_width=numpy.nan))
# [Big output]

You can use the array2string function - docs.

a = numpy.arange(10000).reshape(250,40)
print(numpy.array2string(a, threshold=numpy.nan, max_line_width=numpy.nan))
# [Big output]
舂唻埖巳落 2024-08-23 17:14:05

您并不总是希望打印所有项目,尤其是对于大型数组。

显示更多项目的简单方法:

In [349]: ar
Out[349]: array([1, 1, 1, ..., 0, 0, 0])

In [350]: ar[:100]
Out[350]:
array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1,
       1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])

当切片数组 < 时效果很好。默认为 1000。

You won't always want all items printed, especially for large arrays.

A simple way to show more items:

In [349]: ar
Out[349]: array([1, 1, 1, ..., 0, 0, 0])

In [350]: ar[:100]
Out[350]:
array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1,
       1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])

It works fine when sliced array < 1000 by default.

海未深 2024-08-23 17:14:05

如果您使用的是 Jupyter,请尝试变量检查器扩展。您可以单击每个变量来查看整个数组。

If you are using Jupyter, try the variable inspector extension. You can click each variable to see the entire array.

客…行舟 2024-08-23 17:14:05

matrepr 将打印禁用 max_rowsmax_cols 的整个数组 限制:

from matrepr import mprint

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

mprint(a, max_rows=None, max_cols=None)

结果的前几行:

<250×40, 10000 'int64' elements, array>
       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
    ┌                                                                                                                                                                                                                                                ┐
  0 │  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  │
  1 │  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  │
  2 │  80    81    82    83    84    85    86    87    88    89    90    91    92    93    94    95    96    97    98    99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119  │
  3 │ 120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159  │
  4 │ 160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199  │
  5 │ 200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239  │
  6 │ 240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279  │
  7 │ 280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319  │
  8 │ 320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359  │
  9 │ 360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392   393   394   395   396   397   398   399  │
 10 │ 400   401   402   403   404   405   406   407   408   409   410   411   412   413   414   415   416   417   418   419   420   421   422   423   424   425   426   427   428   429   430   431   432   433   434   435   436   437   438   439  │
 11 │ 440   441   442   443   444   445   446   447   448   449   450   451   452   453   454   455   456   457   458   459   460   461   462   463   464   465   466   467   468   469   470   471   472   473   474   475   476   477   478   479  │

matrepr will print the entire array with disabled max_rows and max_cols limits:

from matrepr import mprint

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

mprint(a, max_rows=None, max_cols=None)

First few lines of the result:

<250×40, 10000 'int64' elements, array>
       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
    ┌                                                                                                                                                                                                                                                ┐
  0 │  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  │
  1 │  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  │
  2 │  80    81    82    83    84    85    86    87    88    89    90    91    92    93    94    95    96    97    98    99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119  │
  3 │ 120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159  │
  4 │ 160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199  │
  5 │ 200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239  │
  6 │ 240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279  │
  7 │ 280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319  │
  8 │ 320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359  │
  9 │ 360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392   393   394   395   396   397   398   399  │
 10 │ 400   401   402   403   404   405   406   407   408   409   410   411   412   413   414   415   416   417   418   419   420   421   422   423   424   425   426   427   428   429   430   431   432   433   434   435   436   437   438   439  │
 11 │ 440   441   442   443   444   445   446   447   448   449   450   451   452   453   454   455   456   457   458   459   460   461   462   463   464   465   466   467   468   469   470   471   472   473   474   475   476   477   478   479  │
甜尕妞 2024-08-23 17:14:05

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

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

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

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

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.

>>> 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.

那一片橙海, 2024-08-23 17:14:05

如果您有 pandas 可用,

    numpy.arange(10000).reshape(250,40)
    print(pandas.DataFrame(a).to_string(header=False, index=False))

可以避免需要重置 numpy.set_printoptions(threshold=sys.maxsize) 的副作用,并且您不会获得 numpy.array 和括号。我发现这可以方便地将宽数组转储到日志文件中

If you have pandas available,

    numpy.arange(10000).reshape(250,40)
    print(pandas.DataFrame(a).to_string(header=False, index=False))

avoids the side effect of requiring a reset of numpy.set_printoptions(threshold=sys.maxsize) and you don't get the numpy.array and brackets. I find this convenient for dumping a wide array into a log file

递刀给你 2024-08-23 17:14:04

使用 numpy.set_printoptions

import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)

Use numpy.set_printoptions:

import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)
那请放手 2024-08-23 17:14:04
import numpy as np
np.set_printoptions(threshold=np.inf)

我建议使用 np.inf 而不是其他人建议的 np.nan 。它们都适合您的目的,但是通过将阈值设置为“无穷大”,每个阅读您的代码的人都可以明显看出您的意思。对我来说,“不是数字”的门槛似乎有点模糊。

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.

倦话 2024-08-23 17:14:04

临时设置

您可以使用 printoptions 上下文管理器:(

with numpy.printoptions(threshold=numpy.inf):
    print(arr)

当然,如果您是这样导入 numpy 的,请将 numpy 替换为 np code>)

上下文管理器(with-块)的使用确保上下文管理器完成后,打印选项将恢复到块开始之前的状态。它确保设置是临时的,并且仅应用于块内的代码。

请参阅 numpy.printoptions 文档有关上下文管理器的详细信息以及它支持的其他参数。它在 NumPy 1.15(2018-07-23 发布)中引入。

Temporary setting

You can use the printoptions context manager:

with numpy.printoptions(threshold=numpy.inf):
    print(arr)

(of course, replace numpy by np if that's how you imported numpy)

The use of a context manager (the with-block) ensures that after the context manager is finished, the print options will revert to whatever they were before the block started. It ensures the setting is temporary, and only applied to code within the block.

See numpy.printoptions documentation for details on the context manager and what other arguments it supports. It was introduced in NumPy 1.15 (released 2018-07-23).

贪恋 2024-08-23 17:14:04

前面的答案是正确的,但作为较弱的替代方案,您可以将其转换为列表:

>>> 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]]

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]]
乖乖兔^ω^ 2024-08-23 17:14:04

这是执行此操作的一次性方法,如果您不想更改默认设置,则此方法很有用:

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

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=numpy.inf)
  pprint(*args, **kwargs)
  numpy.set_printoptions(**opt)
老娘不死你永远是小三 2024-08-23 17:14:04

这听起来像是你正在使用 numpy.

如果是这种情况,您可以添加:

import numpy as np
import sys
np.set_printoptions(threshold=sys.maxsize)

这将禁用角打印。有关详细信息,请参阅此 NumPy 教程

This sounds like you're using numpy.

If that's the case, you can add:

import numpy as np
import sys
np.set_printoptions(threshold=sys.maxsize)

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

合约呢 2024-08-23 17:14:04

按照 Paul Price 建议使用上下文管理器

import numpy as np


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

    def __init__(self, **kwargs):
        kwargs.setdefault('threshold', np.inf)
        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)


if __name__ == '__main__': 
    a = np.arange(1001)

    with fullprint():
        print(a)

    print(a)

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

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):
        kwargs.setdefault('threshold', np.inf)
        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)


if __name__ == '__main__': 
    a = np.arange(1001)

    with fullprint():
        print(a)

    print(a)

    with fullprint(threshold=None, edgeitems=10):
        print(a)
相思故 2024-08-23 17:14:04

numpy.savetxt

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

或者如果您需要字符串:

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

默认输出格式为:

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

并且可以使用更多参数进行配置。

特别注意,这也不显示方括号,并允许进行大量自定义,如以下所述: 如何打印不带括号的 Numpy 数组?

在 Python 2.7.12、numpy 1.11.1 上测试。

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.

Note in particular how this also not shows the square brackets, and allows for a lot of customization, as mentioned at: How to print a Numpy array without brackets?

Tested on Python 2.7.12, numpy 1.11.1.

梦醒时光 2024-08-23 17:14:04

稍作修改:(因为您要打印一个巨大的列表)

import numpy as np
np.set_printoptions(threshold=np.inf, linewidth=200)

x = np.arange(1000)
print(x)

这将增加每行的字符数(默认线宽为 75)。使用适合您的编码环境的任何线宽值。这将使您不必通过在每行添加更多字符来处理大量输出行。

A slight modification: (since you are going to print a huge list)

import numpy as np
np.set_printoptions(threshold=np.inf, linewidth=200)

x = np.arange(1000)
print(x)

This will increase the number of characters per line (default linewidth of 75). Use any value you like for the linewidth which suits your coding environment. This will save you from having to go through huge number of output lines by adding more characters per line.

空城仅有旧梦在 2024-08-23 17:14:04

这是 neok 的轻微修改(删除了向 set_printoptions 传递附加参数的选项)回答。

它展示了如何使用 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)

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

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)

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

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

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)
悸初 2024-08-23 17:14:04
with np.printoptions(edgeitems=50):
    print(x)

将 50 更改为您想要看到的行数

来源: 此处

with np.printoptions(edgeitems=50):
    print(x)

Change 50 to how many lines you wanna see

Source: here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文