在 Numpy 中沿轴 1 查找掩码数组中的连续未掩码数据
要沿给定轴在屏蔽数组中查找连续的未屏蔽数据,请使用 Python Numpy 中的numpy.ma.notmasked_contiguous。该方法返回数组中未屏蔽索引的切片(开始和结束索引)列表。如果输入是 2d 并且指定了轴,则结果是列表列表。
轴是执行操作的轴。如果 None (默认),适用于数组的扁平化版本,这与 flatnotmasked_contiguous 相同。
脚步
首先,导入所需的库 -
import numpy as npimportnumpy.maas ma
使用该方法创建一个包含 int 元素的数组-numpy.array()
arr = np.array([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]])print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)
获取数组的维度 -
print("\nArray Dimensions...\n",arr.ndim)
创建一个屏蔽数组并将其中一些屏蔽为无效 -
maskArr = ma.masked_array(arr, mask =[[1, 1, 0], [ 1, 0, 0], [0, 1, 0], [0, 1, 0]])print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)
获取 Masked Array 的尺寸 -
print("\nOur Masked Array Dimensions...\n",maskArr.ndim)
获取蒙面阵列的形状 -
print("\nOur Masked Array Shape...\n",maskArr.shape)
获取 Masked Array 的元素数量 -
print("\nElements in the Masked Array...\n",maskArr.size)
返回一个布尔值,指示数据是否连续 -
print("\nCheck whether the data is contiguous?\n",maskArr.iscontiguous())
要沿给定轴在掩码数组中查找连续的未掩码数据,请使用 numpy.ma.notmasked_contiguous -
print("\nResult...\n",np.ma.notmasked_contiguous(maskArr, axis = 1))
示例
import numpy as np输出结果importnumpy.maas 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...\n", arr)
print("\nArray type...\n", arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)
# Create a masked array and mask some of them as invalid
maskArr = ma.masked_array(arr, mask =[[1, 1, 0], [ 1, 0, 0], [0, 1, 0], [0, 1, 0]])
print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)
# Get the dimensions of the Masked Array
print("\nOur Masked Array Dimensions...\n",maskArr.ndim)
# Get the shape of the Masked Array
print("\nOur Masked Array Shape...\n",maskArr.shape)
# Get the number of elements of the Masked Array
print("\nElements in the Masked Array...\n",maskArr.size)
# Return a boolean indicating whether the data is contiguous
print("\nCheck whether the data is contiguous?\n",maskArr.iscontiguous())
# To find contiguous unmasked data in a masked array along the given axis, use the numpy.ma.notmasked_contiguous in Python Numpy
print("\nResult...\n",np.ma.notmasked_contiguous(maskArr, axis = 1))
Array...[[65 68 81]
[93 33 39]
[73 88 51]
[62 45 67]]
Array type...
int64
Array Dimensions...
2
Our Masked Array
[[-- -- 81]
[-- 33 39]
[73 -- 51]
[62 -- 67]]
Our Masked Array type...
int64
Our Masked Array Dimensions...
2
Our Masked Array Shape...
(4, 3)
Elements in the Masked Array...
12
Check whether the data is contiguous?
True
Result...
[[slice(2, 3, None)], [slice(1, 3, None)], [slice(0, 1, None), slice(2, 3, None)], [slice(0, 1, None),
slice(2, 3, None)]]
以上是 在 Numpy 中沿轴 1 查找掩码数组中的连续未掩码数据 的全部内容, 来源链接: utcz.com/z/297106.html