查詢NumPy陣列的大小


NumPy 通常與 Python 中的 SciPy 和 matplotlib 等包一起使用。此外,由於 NumPy 中的陣列比 Python 中的列表速度更快,因此該模組被廣泛用於複雜的數學演算法和問題。它具有各種函式和方法,使我們能夠輕鬆簡單地執行矩陣運算。

在 NumPy 陣列中,有兩種型別的陣列:

  • 一維陣列

  • 多維陣列

一維陣列(1D 陣列)

陣列中儲存的元素的資料型別可以是字串、整數、布林值或浮點數。這些陣列稱為單維或一維陣列。如此定義的陣列的形狀將是線性的,通常表示為一維陣列。

多維陣列

這些陣列可以儲存任何資料型別的元素,例如字串、整數、陣列、布林值等。它們包含陣列巢狀在陣列中,也稱為巢狀陣列,並具有非線性結構。

通常,陣列以兩種型別衡量:

  • 陣列的大小

  • 陣列的記憶體大小

陣列大小指的是陣列的長度或該特定陣列中存在的獨立資料元素的總數。

而陣列的記憶體大小指的是陣列在系統硬體中佔用儲存該特定陣列中資料的空間(對於較小的陣列通常以位元組為單位)。

方法 1:使用 itemsize() 函式

itemsize() 函式返回陣列中每個元素佔用的記憶體空間。返回值被認為是以位元組為單位。

語法

<array_object_name>.itemsize

返回整數,表示位元組數。

示例 1

在下面的示例中,我們將使用 itemsize() 函式計算儲存在**一維 NumPy 陣列**中的每個元素佔用的記憶體。除了大小、長度和佔用的總記憶體空間外,還將顯示陣列本身。

演算法

  • **步驟 1** - 定義陣列

  • **步驟 2** - 宣告陣列

  • **步驟 3** - 列印大小

  • **步驟 4** - 列印 itemsize

import numpy

#creation of a numpy array
arr = numpy.array([1,2,3,4,5])

#Displays length of the array
print(f"Length of the array: {numpy.size(arr)}") 

#Displays memory size in bytes
print(f"Space occupied by one element: {arr.itemsize} bytes")

#Total memory space occupied
print(f"Total memory occupied by the element: {arr.itemsize*numpy.size(arr)} bytes")

輸出

Length of the array: 5
Space occupied by one element: 8 bytes
Total memory occupied by the element: 40 bytes

示例 2

在下面的示例中,我們計算了**多維 NumPy 陣列**的相同資訊。

import numpy

#creation of a numpy array
arr = numpy.array([['a','b','c','d'], [1,2,3,4,5]])

arr_2 = numpy.array([ [[1,2,3], 'Tutorials Point'], ['a', 'e', 'i', 'o', 'u']])

#Displays length of the array
print(f'Length of the 2D array: {numpy.size(arr)}')

#Displays memory size in bytes
print(f"Space occupied by one element: {arr.itemsize} bytes")

#Total memory space occupied
print(f"Total memory occupied by the element: {arr.itemsize*numpy.size(arr)} bytes")

print()

print(f"Length of the 3D array: {numpy.size(arr_2)}")
print(f"Space occupied by one element: {arr_2.itemsize} bytes")
print(f"Total memory occupied by the element: {arr_2.itemsize*numpy.size(arr_2)} bytes")

輸出

Length of 2D array: 2
Space occupied by one element: 8 bytes
Total memory occupied by the element: 16 bytes

Length of 3D array: 2
Space occupied by one element: 8 bytes
Total memory occupied by the element: 16 bytes

方法 2:使用 nbytes() 函式

nbytes() 是 Python 中 NumPy 模組中的一個方法。此函式返回 NumPy 陣列佔用的總空間(以位元組為單位),這與返回單個元素佔用的記憶體空間的 itemsize 函式不同。

語法

<array_object_name>.nbytes

返回整數,表示陣列佔用的總位元組空間。

示例 1

在本例中,建立了一個**一維 NumPy 陣列**來解釋佔用的記憶體大小,使用 nbytes 函式。陣列的長度也與之一起顯示。

import numpy

arr = numpy.array([1,2,3,4,5])

#Displays length of the array
print(f"Length of the array: {numpy.size(arr)}") 

#Total memory space occupied
print(f"Total memory occupied by the element: {arr.nbytes} bytes")

輸出

Length of the array: 5
Total memory occupied by the element: 40 bytes

示例 2

在下面的示例中,我們計算了與上面示例中使用的相同資料值的**多維陣列**的長度和記憶體大小。

import numpy

arr = numpy.array([['a','b','c','d'], [1,2,3,4,5]])
arr_2 = numpy.array([ [[1,2,3], 'Tutorials Point'], ['a', 'e', 'i', 'o', 'u']])

#Displays length of the array
print(f'Length of the 2D array: {numpy.size(arr)}')

#Total memory space occupied
print(f"Total memory occupied by the element: {arr.nbytes} bytes")

print()

print(f"Length of the 3D array: {numpy.size(arr_2)}")
print(f"Total memory occupied by the element: {arr_2.nbytes} bytes")

輸出

Length of 2D array: 2
Total memory occupied by the element: 16 bytes

Length of 3D array: 2
Total memory occupied by the element: 16 bytes

結論

與其他資料結構相比,NumPy 陣列消耗的記憶體更少,因此它們用於處理大型資料集並執行復雜的數學計算。itemsize 函式將計算每個元素的大小,而 nbytes 函式將計算整個陣列佔用的記憶體空間。

它們被用於各個領域,例如機器學習、資料分析和計算物理學,將資料轉換為各種形式等等。它也用於資料的各種數值和理論計算。

更新於:2023年8月23日

434 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.