使用 NumPy 從各種物件構建記錄陣列


要顯示 NumPy 記錄陣列,請在 Python NumPy 中使用 **numpy.core.records.array()** 方法。第一個引數是 obj,即輸入。如果 obj 為 None,則呼叫 recarray 建構函式。如果 obj 為字串,則呼叫 fromstring 建構函式。如果 obj 為列表或元組,則如果第一個物件為 ndarray,則呼叫 fromarrays,否則呼叫 fromrecords。如果 obj 為 recarray,則複製 recarray 中的資料(如果 copy=True)並使用新的格式、名稱和標題。如果 obj 為檔案,則呼叫 fromfile。最後,如果 obj 為 ndarray,則返回 obj.view(recarray),如果 copy=True,則複製資料。

步驟

首先,匯入所需的庫 -

import numpy as np

使用 numpy.array() 方法建立一個新陣列 -

arr = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45], [50, 55, 60]])

顯示陣列 -

print("Array...
",arr)

獲取陣列的型別 -

print("
Array type...
", arr.dtype)

獲取陣列的維度:-

print("
Array Dimensions...
", arr.ndim)

要顯示 NumPy 記錄陣列,請在 Python NumPy 中使用 numpy.core.records.array() 方法 -

print("
Record Array...
",np.core.records.array(arr))

示例

import numpy as np

# Create a new array using the numpy.array() method
arr = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45], [50, 55, 60]])

# Display the array
print("Array...
",arr) # Get the type of the array print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
", arr.ndim) # To display the numpy record array, use the numpy.core.records.array() method in Python Numpy print("
Record Array...
",np.core.records.array(arr))

輸出

Array...
[[ 5 10 15]
[20 25 30]
[35 40 45]
[50 55 60]]

Array type...
int64

Array Dimensions...
2

Record Array...
[[ 5 10 15]
[20 25 30]
[35 40 45]
[50 55 60]]

更新於: 2022年2月10日

134 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.