從文字形式的記錄列表建立recarray並在Numpy中設定有效的資料型別
要從文字形式的記錄列表建立recarray,請在Python Numpy中使用**numpy.core.records.fromrecords()**方法。名稱使用“names”引數設定。欄位名稱,可以指定為逗號分隔的字串,格式為'col1, col2, col3',也可以指定為字串列表或元組,格式為['col1', 'col2', 'col3']。可以使用空列表,在這種情況下,將使用預設欄位名稱('f0','f1',…)。資料型別使用“dtype”引數設定。
第一個引數是資料,同一欄位中的資料可能是異構的——它們將被提升到最高資料型別。dtype是所有陣列的有效dtype。formats、names、titles、aligned、byteorder引數,如果dtype為None,則這些引數將傳遞給numpy.format_parser來構造dtype。如果formats和dtype都為None,則將自動檢測formats。使用元組列表而不是列表列表可以加快處理速度。
步驟
首先,匯入所需的庫:
import numpy as np
使用numpy.array()方法建立一個新陣列:
arr1 = np.array([[7, 14, 21], [30, 37, 45]]) arr2 = np.array([[11.3, 18.7, 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)要從文字形式的記錄列表建立recarray,請在Python Numpy中使用numpy.core.records.fromrecords()方法。資料型別使用“dtype”引數設定:
print("
Record Array...
",np.core.records.fromrecords([arr1,arr2,arr3], names = 'col1, col2,
col3', dtype=np.dtype(('a', np.int32), ('b', np.float32), ('c','S3' ))))示例
import numpy as np
# Create a new array using the numpy.array() method
arr1 = np.array([[7, 14, 21], [30, 37, 45]])
arr2 = np.array([[11.3, 18.7, 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 recarray from a list of records in text form, use the numpy.core.records.fromrecords() 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.
# The datatype is set using the "dtype" parameter
print("
Record Array...
",np.core.records.fromrecords([arr1,arr2,arr3], names = 'col1, col2, col3', dtype=np.dtype(('a', np.int32), ('b', np.float32), ('c','S3' ))))輸出
Array1... [[ 7 14 21] [30 37 45]] Array2... [[11.3 18.7 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... [[[b'\x07\x00\x00\x00' b'\x0E\x00\x00\x00' b'\x15\x00\x00\x00'] [b'\x1E\x00\x00\x00' b'\x25\x00\x00\x00' b'\x2D\x00\x00\x00']] [[b'\x9A\x99\x99\x99' b'\x33\x33\x33\x33' b'\x00\x00\x00\x00'] [b'\x00\x00\x00\x00' b'\x00\x00\x00\x00' b'\xCD\xCC\xCC\xCC']] [[b'\x31\x00\x00\x00' b'\x62\x00\x00\x00' b'\x6A\x00\x00\x00'] [b'\x35\x00\x00\x00' b'\x32\x00\x00\x00' b'\x6B\x00\x00\x00']]]
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP