打印完整的 NumPy 数组

Muhammad Maisam Abbas 2021年10月2日
打印完整的 NumPy 数组

本教程将介绍如何在 Python 中打印完整的 NumPy 数组。

使用 Python 中的 numpy.set_printoptions() 函数打印完整的 NumPy 数组

默认情况下,如果我们的数组长度很大,Python 会在打印数组时截断输出。下面的代码示例演示了这种现象。

import numpy as np

array = np.arange(10000)
print(array)

输出:

[   0    1    2 ... 9997 9998 9999]

在上面的代码中,我们首先使用 Python 中的 np.arange() 函数创建了一个 NumPy 数组 array,其中包含从 0 到 9999 的数值。然后我们使用 print() 函数打印数组的元素。我们得到一个截断的输出,因为数组太大而无法完全显示。

这个问题可以通过 numpy.set_printoptions() 函数来解决。它在 Python 中设置与打印数组相关的不同参数。我们可以使用 numpy.set_printoptions() 函数的 threshold 参数来 sys.maxsize 打印完整的 NumPy 数组。要使用 sys.maxsize 属性,我们还必须导入 sys 库。下面的代码示例显示了如何在 Python 中使用 numpy.set_printoptions() 函数和 sys.maxsize 属性打印完整的 NumPy 数组。

import sys
import numpy as np

array = np.arange(10001)
np.set_printoptions(threshold=sys.maxsize)
print(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    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   100   101   102   103   104   105   106   107
   108   109   110   111   112   113   114   115   116   117   118   119
...
  9912  9913  9914  9915  9916  9917  9918  9919  9920  9921  9922  9923
  9924  9925  9926  9927  9928  9929  9930  9931  9932  9933  9934  9935
  9936  9937  9938  9939  9940  9941  9942  9943  9944  9945  9946  9947
  9948  9949  9950  9951  9952  9953  9954  9955  9956  9957  9958  9959
  9960  9961  9962  9963  9964  9965  9966  9967  9968  9969  9970  9971
  9972  9973  9974  9975  9976  9977  9978  9979  9980  9981  9982  9983
  9984  9985  9986  9987  9988  9989  9990  9991  9992  9993  9994  9995
  9996  9997  9998  9999 10000]

在上面的代码中,我们首先使用 numpy.arange() 函数创建了一个 NumPy 数组 array,其中包含从 0 到 10000 的元素。我们使用 np.set_printoptions(threshold = sys.maxsize) 函数将数组的打印选项设置为最大值。然后我们在 Python 中使用简单的 print() 函数打印了完整的数组。

我们的问题还有另一种解决方案,它只涉及使用 NumPy 库。我们可以在 numpy.set_printoptions() 函数中指定 threshold 等于 np.inf 以在 Python 中打印完整的数组。np.inf 属性指定 print() 将无限运行,直到打印整个数组。请参考以下代码示例。

import numpy as np

array = np.arange(10001)
np.set_printoptions(threshold=np.inf)
print(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    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   100   101   102   103   104   105   106   107
   108   109   110   111   112   113   114   115   116   117   118   119
...
  9912  9913  9914  9915  9916  9917  9918  9919  9920  9921  9922  9923
  9924  9925  9926  9927  9928  9929  9930  9931  9932  9933  9934  9935
  9936  9937  9938  9939  9940  9941  9942  9943  9944  9945  9946  9947
  9948  9949  9950  9951  9952  9953  9954  9955  9956  9957  9958  9959
  9960  9961  9962  9963  9964  9965  9966  9967  9968  9969  9970  9971
  9972  9973  9974  9975  9976  9977  9978  9979  9980  9981  9982  9983
  9984  9985  9986  9987  9988  9989  9990  9991  9992  9993  9994  9995
  9996  9997  9998  9999 10000]

我们使用 np.set_printoptions() 函数将 threshold 参数设置为 np.inf。然后我们在 Python 中使用简单的 print() 函数打印了完整的数组。这种方法优于前一种方法,因为这种方法只需要 NumPy 库。

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn