通过在 Numpy 中传递标量布尔值将掩码初始化为齐次布尔数组
通过传递标量布尔值初始化掩码,使其具有与数据相同的形状,并成为齐次布尔数组。True 指示掩码数据(例如:无效数据)。“mask”参数用于设置掩码。使用 ma.MaskedArray() 方法创建掩码数组。
掩码数组是标准 numpy.ndarray 和掩码的组合。掩码要么是 nomask,表示关联数组的任何值均有效,要么是布尔数组,它决定关联数组的每个元素的值是否有效。
步骤
首先,导入所需库 −
import numpy as np import numpy.ma as ma
使用 numpy.array() 方法创建包含 int 元素的数组 −
arr = np.array([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]])
print("Array...
", arr)
print("
Array type...
", arr.dtype)获取数组的维度 −
print("
Array Dimensions...
",arr.ndim)
创建一个掩码数组。“mask”参数用于设置掩码。通过传递标量布尔值初始化掩码,使其具有与数据相同的形状,并成为齐次布尔数组。True 指示掩码数据(例如:无效数据)
maskArr = ma.MaskedArray(arr, mask =True)
print("
Our Masked Array
", maskArr)
print("
Our Masked Array type...
", maskArr.dtype)获取掩码数组的维度 −
print("
Our Masked Array Dimensions...
",maskArr.ndim)
获取掩码数组的形状 −
print("
Our Masked Array Shape...
",maskArr.shape)获取掩码数组的元素数 −
print("
Elements in the Masked Array...
",maskArr.size)
示例
# Python ma.MaskedArray - Initialize the mask to homogeneous boolean array by passing in a scalar boolean value
import numpy as np
import numpy.ma as ma
# Create an array with int elements using the numpy.array() method
arr = np.array([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]])
print("Array...
", arr)
print("
Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Create a masked array
# The mask is set using the "mask" parameter
# The mask initialized to homogeneous boolean array with the same shape as data by passing in a scalar boolean value:
# True indicates a masked (i.e. invalid) data.
maskArr = ma.MaskedArray(arr, mask =True)
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)
# 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)输出
Array... [[65 68 81] [93 33 39] [73 88 51] [62 45 67]] Array type... int64 Array Dimensions... 2 Our Masked Array [[-- -- --] [-- -- --] [-- -- --] [-- -- --]] Our Masked Array type... int64 Our Masked Array Dimensions... 2 Our Masked Array Shape... (4, 3) Elements in the Masked Array... 12
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP