简体   繁体   English

面膜与numpy isin

[英]Mask with numpy isin

I want to make a mask with numpy array I've foun a function, but it doesn't make what I want.Here is the code example. 我想用numpy array做一个蒙版,我找到了一个函数,但它没有达到我想要的效果。这是代码示例。

np.isin([1,2,3,4,5,8,6,1,1],[1,2,3,5,1])

This code returns this: 此代码返回以下内容:

array([ True,  True,  True, False,  True, False, False,  True,  True], dtype=bool)

But I want the same output except the last value of output array to be False. 但是我希望除了输出数组的最后一个值是False以外的相同输出。 Because I need exact mask of sequence( [1,2,3,5,1] ) in this order and no longer than its len 因为我需要此顺序的精确掩码sequence( [1,2,3,5,1] ),并且不超过其len

You can turn elements after certain amount of True s to zero with: 您可以使用以下方法将一定数量的True后的元素设置为零:

mask[mask.cumsum() > 5] = False
#                    ^ length of the second array

import numpy as np
mask = np.isin([1,2,3,4,5,8,6,1,1],[1,2,3,5,1])

mask[mask.cumsum() > 5] = False
mask
# array([ True,  True,  True, False,  True, False, False,  True, False], dtype=bool)

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

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