如果 NumPy 数组没有命名字段,则获取或设置数组的掩码


要获取或设置数组的掩码(如果它没有命名字段),请在 Python NumPy 中使用 **MaskedArray.recordmask**。对于结构化数组,返回一个布尔值 ndarray,其中条目如果所有字段都被掩盖则为 True,否则为 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)

创建一个掩码数组 -

arr = ma.array(arr)

arr[0, 1] = ma.masked
arr[1, 1] = ma.masked
arr[2, 1] = ma.masked
arr[2, 2] = ma.masked
arr[3, 0] = ma.masked
arr[3, 2] = ma.masked
arr[3, 3] = ma.masked

沿特定轴计算掩码元素的数量 -

print("
The number of masked elements...
",ma.count_masked(arr, axis = 1))

返回掩码数组的掩码 -

print("
The mask of a masked array...
",ma.getmask(arr))

将掩码数组的数据作为 ndarray 返回 -

print("
Data of a masked array as an ndarray...
",ma.getdata(arr))

确定输入是否为掩码数组的实例 -

print("
Whether input is an instance of masked array?
",ma.isMaskedArray(arr))

要获取或设置数组的掩码(如果它没有命名字段),请使用 mMaskedArray.recordmask -

print("
Result...
",arr.recordmask)

示例

# Python ma.MaskedArray - Get or set the mask of the array if it has no named fields

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 arr = ma.array(arr) arr[0, 1] = ma.masked arr[1, 1] = ma.masked arr[2, 1] = ma.masked arr[2, 2] = ma.masked arr[3, 0] = ma.masked arr[3, 2] = ma.masked arr[3, 3] = ma.masked # Count the number of masked elements along specific axis print("
The number of masked elements...
",ma.count_masked(arr, axis = 1)) # Return the mask of a masked array print("
The mask of a masked array...
",ma.getmask(arr)) # Return the data of a masked array as an ndarray print("
Data of a masked array as an ndarray...
",ma.getdata(arr)) # Determine whether input is an instance of masked array print("
Whether input is an instance of masked array?
",ma.isMaskedArray(arr)) # To get or set the mask of the array if it has no named fields, use the mMaskedArray.recordmask in Python Numpy print("
Result...
",arr.recordmask)

输出

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...
[1 1 2 3]

The mask of a masked array...
[[False True False False]
[False True False False]
[False True True False]
[ True False True True]]

Data of a masked array as an ndarray...
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]

Whether input is an instance of masked array?
True

Result...
[[False True False False]
[False True False False]
[False True True False]
[ True False True True]]

更新于: 2022年2月3日

86 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告