获取NumPy中掩码数组的内存布局信息
要获取掩码数组的内存布局信息,请使用NumPy中的**ma.MaskedArray.flags**。掩码数组是可以包含缺失或无效条目的数组。 numpy.ma 模块提供了几乎与NumPy相同的替代方案,支持带有掩码的数据数组。
掩码数组是标准numpy.ndarray和掩码的组合。掩码要么是nomask,表示相关数组中没有无效值;要么是一个布尔型数组,用于确定相关数组的每个元素是否有效。
步骤
首先,导入所需的库:
import numpy as np import numpy.ma as ma
使用numpy.array()方法创建一个NumPy数组:
arr = np.array([[35, 85], [67, 33]]) print("Array...
", arr) print("
Array type...
", arr.dtype)
获取数组的维度:
print("Array Dimensions...",arr.ndim)
获取数组的内存布局信息:
print("
Array flags...
",arr.flags)
创建一个掩码数组,并将其中一些标记为无效:
maskArr = ma.masked_array(arr, mask =[[0, 0], [ 0, 1]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)
获取掩码数组的维度:
print(" Our Masked Array Dimensions...",maskArr.ndim)
要获取掩码数组的内存布局信息,请使用NumPy中的ma.MaskedArray.flags:
print("
Our Masked Array flags...
",maskArr.flags)
示例
import numpy as np import numpy.ma as ma # Create a numpy array using the numpy.array() method arr = np.array([[35, 85], [67, 33]]) print("Array...
", arr) print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("Array Dimensions...",arr.ndim) # Get the information about the memory layout of the array print("
Array flags...
",arr.flags) # 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 dimensions of the Masked Array print(" Our Masked Array Dimensions...",maskArr.ndim) # To get the information about the memory layout of the masked array, use the ma.MaskedArray.flags in Numpy print("
Our Masked Array flags...
",maskArr.flags)
输出
Array... [[35 85] [67 33]] Array type... int64 Array Dimensions... 2 Array flags... C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False Our Masked Array [[35 85] [67 --]] Our Masked Array type... int64 Our Masked Array Dimensions... 2 Our Masked Array flags... C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : False WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False
广告