[英]Express 1d masked array as list of slices
I have a 1d numpy array with booleans values ( mask
) which I would like to convert into a list of slices where the mask is True, eg:我有一个带有布尔值(
mask
)的 1d numpy 数组,我想将其转换为掩码为 True 的切片列表,例如:
mask = [False, True, True, True, False, False, True, True]
and I would like to obtain我想获得
[slice(1, 4, None), slice(6, 8, None)]
The numpy masked array operations (in particular np.ma.clump_masked()
) can do that, but the only way I found to use it would be to do the following: numpy 屏蔽数组操作(特别是
np.ma.clump_masked()
)可以做到这一点,但我发现使用它的唯一方法是执行以下操作:
np.ma.clump_masked(np.ma.masked_array(np.ones_like(mask), mask))
which yields exactly what I'm looking for:这正是我正在寻找的:
[slice(1, 4, None), slice(6, 8, None)]
[切片(1、4、无)、切片(6、8、无)]
ie, generating an array with the same shape as mask
, applying the mask to it, and then computing mask_clumped()
on that.即,生成一个与
mask
具有相同形状的数组,对其应用掩码,然后在其上计算mask_clumped()
。
However, the np.ma.masked_array(np.ones_like(mask), mask)
-step seems unnecessary to me.但是,
np.ma.masked_array(np.ones_like(mask), mask)
对我来说似乎是不必要的。 Is there any way to obtain the list of slices from a simplified operation which I would imagine to look like the following?有什么方法可以从我想象如下所示的简化操作中获取切片列表?
np.ma.clump_masked(mask)
np.ma.masked_array
requires a masked array as input, not an ndarray
. np.ma.masked_array
需要一个屏蔽数组作为输入,而不是ndarray
。 One approach is to do what you're currently doing and create a masked array一种方法是做你目前正在做的事情并创建一个屏蔽数组
import numpy as np
mask = np.asarray([False, True, True, True, False, False, True, True])
masked_array = np.ma.masked_array(data=mask, mask=mask)
np.ma.clump_masked(masked_array)
However, I assume you're generating mask
based on some condition?但是,我假设您是根据某些条件生成
mask
? In which case, you can use np.ma.masked_where
.在这种情况下,您可以使用
np.ma.masked_where
。 For example, to get all the slices of each even number from 0 to 9:例如,要获取从 0 到 9 的每个偶数的所有切片:
import numpy as np
arr = np.arange(10)
masked_arr = np.ma.masked_where(arr % 2 == 0, arr)
np.ma.clump_masked(masked_arr)
which outputs:输出:
[slice(0, 1, None),
slice(2, 3, None),
slice(4, 5, None),
slice(6, 7, None),
slice(8, 9, None)]
There are other functions such as np.ma.masked_inside
which will create a masked array and mask all elements within some interval.还有其他函数,例如
np.ma.masked_inside
,它将创建一个屏蔽数组并屏蔽某个间隔内的所有元素。 Check the 'see also' of the masked_where
docs for a list of the related funcitons.查看
masked_where
文档的“另请参阅”以获取相关函数的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.