当掩码等于nomask时,返回掩码数组的掩码
要返回掩码数组的掩码,请在Python Numpy中使用**ma.getmaskarray()**方法。如果arr是MaskedArray且掩码不是nomask,则返回arr的掩码作为ndarray;否则,返回与arr形状相同的全False布尔数组。
掩码数组是标准numpy.ndarray和掩码的组合。掩码要么是nomask(表示关联数组中没有无效值),要么是一个布尔数组,用于确定关联数组的每个元素的值是否有效。
步骤
首先,导入所需的库:
import numpy as np import numpy.ma as ma
使用numpy.arange()方法创建一个包含整数元素的4x4数组:
arr = np.arange(16).reshape((4,4)) print("Array...
", arr) print("
Array type...
", arr.dtype)
获取数组的维度:
print("
Array Dimensions...
",arr.ndim)
获取数组的形状:
print("
Our Masked Array Shape...
",arr.shape)
获取数组的元素个数:
print("
Elements in the Masked Array...
",arr.size)
创建一个掩码数组。此处,mask == nomask:
arr = ma.array(arr)
要沿特定轴计算掩码元素的数量,请使用ma.MaskedArray.count_masked()方法:
print("
The number of masked elements...
",ma.count_masked(arr))
要返回掩码数组的掩码或全False布尔数组,请在Python Numpy中使用ma.getmaskarray()方法:
print("
Result (mask of a masked array)...
",ma.getmaskarray(arr))
示例
import numpy as np import numpy.ma as ma # Creating a 4x4 array with int elements using the numpy.arange() method arr = np.arange(16).reshape((4,4)) print("Array...
", arr) print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) print("
Our Array type...
", arr.dtype) # Get the shape of the Array print("
Our Masked Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Masked Array...
",arr.size) # Create a masked array # Here, mask == nomask arr = ma.array(arr) # To count the number of masked elements along specific axis, use the ma.MaskedArray.count_masked() print("
The number of masked elements...
",ma.count_masked(arr)) # To return the mask of a masked array, or full boolean array of False, use the ma.getmaskarray() method in Python Numpy print("
Result (mask of a masked array)...
",ma.getmaskarray(arr))
输出
Array... [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] Array type... int64 Array Dimensions... 2 Our Array type... int64 Our Masked Array Shape... (4, 4) Elements in the Masked Array... 16 The number of masked elements... 0 Result (mask of a masked array)... [[False False False False] [False False False False] [False False False False] [False False False False]]
广告