Python numpy.ma.notmasked_contiguous函数
numpy.ma.notmasked_contiguous()函数在一个被屏蔽的数组中沿着给定的轴找到连续的未被屏蔽的数据。
语法: numpy.ma.notmasked_contiguous(arr, axis = None)
参数 :
arr : [array_like] 输入阵列。
axis : [int, optional] 执行操作的轴。默认为无。
返回 : [list] 阵列中未被屏蔽的索引的片断(开始和结束索引)的列表。如果输入是2d并且指定了轴,结果是一个列表。
代码#1:
# Python program explaining
# numpy.ma.notmasked_contiguous() function
# importing numpy as geek
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
arr = geek.arange(12).reshape((3, 4))
mask = geek.zeros_like(arr)
mask[1:, :-1] = 1; mask[0, 1] = 1; mask[-1, 0] = 0
ma = geek.ma.array(arr, mask = mask)
gfg = geek.ma.notmasked_contiguous(ma)
print (gfg)
输出 :
[slice(0, 1, None), slice(2, 4, None), slice(7, 9, None), slice(11, 12, None)]
代码#2:
# Python program explaining
# numpy.ma.notmasked_contiguous() function
# importing numpy as geek
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
arr = geek.arange(12).reshape((3, 4))
mask = geek.zeros_like(arr)
mask[1:, :-1] = 1; mask[0, 1] = 1; mask[-1, 0] = 0
ma = geek.ma.array(arr, mask = mask)
gfg = geek.ma.notmasked_contiguous(ma, axis = 1)
print (gfg)
输出 :
[[slice(0, 1, None), slice(2, 4, None)], [slice(3, 4, None)], [slice(0, 1, None), slice(3, 4, None)]]