简体   繁体   English

数组中的掩码索引

[英]Mask Indices in an array

I have an array like:我有一个像这样的数组:

data=np.array([1,2,3,5,8,7,2,1,3,5,1,2,20])

I would like to mask an array indices with a step of 3. For an example I able to mask where the value of an array equal to 3.我想以 3 的步长屏蔽数组索引。例如,我可以屏蔽数组的值等于 3 的位置。

import numpy as np
import numpy.ma as ma
x = np.array([1,2,3,5,8,7,2,1,3,5,1,2,20])
mx=ma.masked_values(x,3)
output:
[1 2 -- 5 8 7 2 1 -- 5 1 2 20]

Requirement: I need to mask the every 3nd Indices in an array.(step of 3)要求:我需要屏蔽数组中的每 3 个索引。(第 3 步)

Required Output:所需 Output:

[1,2,3,--,8,7,--,1,3,--,1,2,--]

Let us create a mask based on the indices of the array:让我们根据数组的索引创建一个掩码:

np.ma.masked_array(x, np.r_[1, 1:len(x)] % 3 == 0)

masked_array(data=[1, 2, 3, --, 8, 7, --, 1, 3, --, 1, 2, --],
             mask=[False, False, False,  True, False, False,  True, False,
                   False,  True, False, False,  True],
       fill_value=999999)

try this:尝试这个:

mask = list(map(lambda i: i%3==2, np.arange(len(x))))
ma.masked_array(x, mask=mask)

Output: Output:

masked_array(data=[1, 2, --, 5, 8, --, 2, 1, --, 5, 1, --, 20],
             mask=[False, False,  True, False, False,  True, False, False,
                    True, False, False,  True, False],
       fill_value=999999)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM