I have a numpy array
array = np.array([5,100,100,100,5,5,100,100,100,5])
I create a mask with boolean indexing like so:
mask = (array < 30)
This gives a mask like
[ True False False False True True False False False True]
I can get the indices of the True
values in the mask with
indices = np.where(mask)[0]
This gives
[0 4 5 9]
For every True
value in the mask, I would like to modify the next 2 elements to also be True
.
I can do this with a for
loop like so:
for i in indices:
mask[i:i+3] = True
Is there a more numpythonic approach to this without using a for
loop?
Desired mask output:
[ True True True False True True True True False True]
The main priority here is performance.