列印完整 NumPy 陣列而不截斷


NumPy 是一個功能強大的 Python 庫,用於處理大型多維陣列。但是,在列印大型 NumPy 陣列時,直譯器通常會截斷輸出以節省空間,並且僅顯示該陣列的幾個元素。在本文中,我們將展示如何列印完整的 NumPy 陣列而不截斷。

為了正確理解問題陳述,請考慮以下示例

輸入

aray = np.arange(1100)

輸出

[   0    1    2 ... 1097 1098 1099]

在上面的示例中,我們建立了一個包含 1100 個元素的陣列。當我們列印它時,Python 直譯器會自動將其截斷,只顯示一小部分輸出,如上所示。它使用三個點(…)來指定某些元素未顯示。

Python 程式列印完整的 NumPy 陣列而不截斷

要列印 NumPy 陣列的所有元素而不截斷,我們可以使用以下方法

  • set_printoptions()

  • array2string()

使用 set_printoptions()

NumPy 的 set_printoptions() 方法允許我們修改列印陣列時顯示的方式。要列印整個陣列,我們將 'threshold' 引數設定為 'np.inf'。

示例 1

以下示例說明了如何使用 set_printoptions() 列印完整的 NumPy 陣列而不截斷。

方法

  • 第一步是匯入 NumPy 包,並使用引用名稱 'np'。

  • 接下來,我們使用 random.rand() 方法建立一個 NumPy 陣列,以生成一個大小為 5x3 的隨機陣列,其中填充的值介於 0 和 1 之間。

  • 現在,使用 set_printoptions() 透過將 threshold 引數設定為 np.inf 來顯示整個陣列。這裡,np.inf 表示沒有閾值。

  • 最後,我們使用內建方法 print() 來顯示結果。

import numpy as np
# generating a random NumPy array
aray = np.random.rand(5, 3)
# Setting the print options 
np.set_printoptions(threshold = np.inf)
# to print the generated array 
print(aray)

輸出

[[0.20389255 0.29142771 0.26634027]
 [0.95128389 0.11253549 0.55029953]
 [0.42578651 0.26762357 0.0058134 ]
 [0.47427662 0.080491   0.98576308]
 [0.29514599 0.44207715 0.51177261]]

示例 2

在本例中,我們不會生成隨機陣列,而是使用 NumPy 的內建方法 arange() 建立一個包含從 0 開始的 40 個連續值的陣列。arange() 方法返回一個 NumPy 陣列。

import numpy as np
# creating a NumPy array with 40 values
aray = np.arange(40)
# Setting the print options here
np.set_printoptions(threshold = np.inf)
# to display the result
print(aray)

輸出

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

示例 3

這是另一個示例,展示瞭如何列印完整的 NumPy 陣列而不截斷。在這個特定的示例中,我們將使用 random.rand() 方法建立 NumPy 陣列,就像我們在第一個示例中所做的那樣。透過將 threshold 設定為 'sys.maxsize' 使用 set_printoptions(),我們將顯示整個陣列。這裡,sys.maxsize 屬性提供了當前系統中可以表示為整數的最大值。

# importing the required packages
import numpy as np
import sys
# generating a random NumPy array
aray = np.random.rand(5, 3)
# Setting the print options 
np.set_printoptions(threshold = sys.maxsize)
# to display the result
print(aray)

輸出

[[0.83641797 0.63958049 0.22548871]
 [0.07813775 0.75781466 0.0283849 ]
 [0.79322127 0.2648928  0.19495721]
 [0.44913602 0.17464489 0.67625814]
 [0.55415935 0.40091674 0.1828985 ]]

使用 array2string()

NumPy 的 array2string() 方法將給定陣列轉換為字串表示形式。同樣,要列印整個陣列,我們將傳遞給定的陣列作為引數,並將名為 'threshold' 的附加引數設定為 'np.inf'。'np.inf' 表示沒有截斷閾值。

示例

在下面的示例中,我們將建立一個大小為 5x5 的隨機 NumPy 陣列,其中填充的值介於 0 和 1 之間。要列印整個陣列而不截斷,我們將使用 np.array2string() 方法,傳遞 NumPy 陣列並將 threshold 引數設定為 np.inf。

import numpy as np
# generating a random NumPy array
aray = np.random.rand(5, 5)
# Converting the array to a string 
arr_st = np.array2string(aray, threshold = np.inf)
# to print the converted array string
print(arr_st)

輸出

[[0.20558096 0.63450548 0.84019308 0.80807701 0.50960877]
 [0.30689032 0.54547474 0.23338944 0.67067941 0.81017394]
 [0.69467855 0.57111774 0.98147844 0.4711527  0.85500914]
 [0.83951451 0.83768907 0.027058   0.06053751 0.57541982]
 [0.45509108 0.6337008  0.65374078 0.38031754 0.0921497 ]]

結論

在本文中,我們學習了兩種使用示例程式列印完整 NumPy 陣列而不截斷的方法。這兩種方法是 array2string() 和 set_printoptions() 方法。它們都採用一個名為 'threshold' 的引數,該引數可以設定為 'np.inf' 或 'sys.maxsize' 以列印 NumPy 陣列的所有元素而不截斷。

更新於: 2023-07-21

7K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.