使用 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]]
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP