获取在 NumPy 中遍历时每个维度步长的字节元组
要获取在遍历数组时每个维度步长的字节元组,请使用 NumPy 中的 **ma.MaskedArray.strides** 属性。数组 *a* 中元素 (i[0], i[1], ..., i[n]) 的字节偏移量为:
offset = sum(np.array(i) * a.strides)
掩码要么是 nomask,表示关联数组的无任何值无效,要么是一个布尔值数组,它决定关联数组的每个元素的值是否有效。
步骤
首先,导入所需的库:
import numpy as np import numpy.ma as ma
使用 numpy.array() 方法创建一个数组:
arr = np.array([[35, 85], [67, 33]])
print("Array...
", arr)
print("
Array type...
", arr.dtype)
print("
Array itemsize...
", arr.itemsize)获取数组的维度:
print("Array Dimensions...
",arr.ndim)创建一个掩码数组并将其中一些标记为无效:
maskArr = ma.masked_array(arr, mask =[[0, 0], [ 0, 1]])
print("
Our Masked Array
", maskArr)
print("
Our Masked Array type...
", maskArr.dtype)获取掩码数组的 itemsize:
print("
Our Masked Array itemsize...
", maskArr.itemsize)获取掩码数组的维度:
print("
Our Masked Array Dimensions...
",maskArr.ndim)获取掩码数组的形状:
print("
Our Masked Array Shape...
",maskArr.shape)
获取掩码数组的元素数量:
print("
Elements in the Masked Array...
",maskArr.size)要获取在遍历数组时每个维度步长的字节元组,请使用 NumPy 中的 ma.MaskedArray.strides 属性
print("
Strides...
",maskArr.strides)示例
import numpy as np
import numpy.ma as ma
arr = np.array([[35, 85], [67, 33]])
print("Array...
", arr)
print("
Array type...
", arr.dtype)
print("
Array itemsize...
", arr.itemsize)
# Get the dimensions of the Array
print("Array Dimensions...
",arr.ndim)
# Create a masked array and mask some of them as invalid
maskArr = ma.masked_array(arr, mask =[[0, 0], [ 0, 1]])
print("
Our Masked Array
", maskArr)
print("
Our Masked Array type...
", maskArr.dtype)
# Get the itemsize of the Masked Array
print("
Our Masked Array itemsize...
", maskArr.itemsize)
# Get the dimensions of the Masked Array
print("
Our Masked Array Dimensions...
",maskArr.ndim)
# Get the shape of the Masked Array
print("
Our Masked Array Shape...
",maskArr.shape)
# Get the number of elements of the Masked Array
print("
Elements in the Masked Array...
",maskArr.size)
# To get the Tuple of bytes to step in each dimension when traversing an array, use the ma.MaskedArray.strides attribute in Numpy
print("
Strides...
",maskArr.strides)输出
Array... [[35 85] [67 33]] Array type... int64 Array itemsize... 8 Array Dimensions... 2 Our Masked Array [[35 85] [67 --]] Our Masked Array type... int64 Our Masked Array itemsize... 8 Our Masked Array Dimensions... 2 Our Masked Array Shape... (2, 2) Elements in the Masked Array... 4 Strides... (16, 8)
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP