從NumPy中扁平的陣列列表建立記錄陣列並根據索引獲取特定值


要從扁平的陣列列表建立一個記錄陣列,請在Python NumPy中使用**numpy.core.records.fromarrays()**方法。名稱使用**"names"**引數設定。欄位名稱,可以以'col1, col2, col3'形式的逗號分隔字串指定,也可以以['col1', 'col2', 'col3']形式的字串列表或元組指定。可以使用空列表,在這種情況下使用預設欄位名稱('f0','f1',…)。

它返回由給定arrayList列組成的記錄陣列。第一個引數是陣列式物件的列表(例如列表、元組和ndarray)。dtype是所有陣列的有效dtype。如果dtype為None,則formats、names、titles、aligned、byteorder引數將傳遞給numpy.format_parser以構造dtype。

步驟

首先,匯入所需的庫:

import numpy as np

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

arr1 = np.array([[5, 10, 15], [20, 25, 30]])
arr2 = np.array([[9, 18, 24], [87.5, 65, 23.8]])
arr3 = np.array([['12', 'bbb', 'john'], ['5.6', '29', 'k']])

顯示陣列:

print("Array1...
",arr1) print("Array2...
",arr2) print("Array3...
",arr3)

獲取陣列的型別:

print("
Array1 type...
", arr1.dtype) print("
Array2 type...
", arr2.dtype) print("
Array3 type...
", arr3.dtype)

獲取陣列的維度:

print("
Array1 Dimensions...
", arr1.ndim) print("
Array2 Dimensions...
", arr2.ndim) print("
Array3 Dimensions...
", arr3.ndim)

要從扁平的陣列列表建立一個記錄陣列,請使用numpy.core.records.fromarrays()方法:

rec = np.core.records.fromarrays([arr1,arr2,arr3], names = 'a,b,c')
print("
Record Array...
",rec)

讓我們嘗試獲取值:

print("
Fetching the values...
",rec[0]) print("
Fetching the values...
",rec[1])

示例

import numpy as np

# Create a new array using the numpy.array() method
arr1 = np.array([[5, 10, 15], [20, 25, 30]])
arr2 = np.array([[9, 18, 24], [87.5, 65, 23.8]])
arr3 = np.array([['12', 'bbb', 'john'], ['5.6', '29', 'k']])

# Display the arrays
print("Array1...
",arr1) print("Array2...
",arr2) print("Array3...
",arr3) # Get the type of the arrays print("
Array1 type...
", arr1.dtype) print("
Array2 type...
", arr2.dtype) print("
Array3 type...
", arr3.dtype) # Get the dimensions of the Arrays print("
Array1 Dimensions...
", arr1.ndim) print("
Array2 Dimensions...
", arr2.ndim) print("
Array3 Dimensions...
", arr3.ndim) # To create a record array from a (flat) list of array, use the numpy.core.records.fromarrays() method in Python Numpy # The names is set using the "names" parameter # The field names, either specified as a comma-separated string in the form 'col1, col2, col3', or as a list or tuple of strings in the form ['col1', 'col2', 'col3']. # An empty list can be used, in that case default field names (‘f0’, ‘f1’, …) are used. rec = np.core.records.fromarrays([arr1,arr2,arr3], names = 'a,b,c') print("
Record Array...
",rec) print("
Fetching the values...
",rec[0]) print("
Fetching the values...
",rec[1])

輸出

Array1...
[[ 5 10 15]
[20 25 30]]
Array2...
[[ 9. 18. 24. ]
[87.5 65. 23.8]]
Array3...
[['12' 'bbb' 'john']
['5.6' '29' 'k']]

Array1 type...
int64

Array2 type...
float64

Array3 type...
<U4

Array1 Dimensions...
2

Array2 Dimensions...
2

Array3 Dimensions...
2

Record Array...
[[( 5, 9. , '12') (10, 18. , 'bbb') (15, 24. , 'john')]
[(20, 87.5, '5.6') (25, 65. , '29') (30, 23.8, 'k')]]

Fhing the values...
[( 5, 9., '12') (10, 18., 'bbb') (15, 24., 'john')]

Fhing the values...
[(20, 87.5, '5.6') (25, 65. , '29') (30, 23.8, 'k')]

更新於:2022年2月17日

121 次檢視

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.