[英]Masked values in numpy digitize
我希望numpy digitize
忽略我数组中的一些值。 为了实现这一点,我用NaN
替换了不需要的值并掩盖了NaN
值:
import numpy as np
A = np.ma.array(A, mask=np.isnan(A))
尽管如此, np.digitize
将掩码值抛出为-1
。 有没有其他方法可以让np.digitize
忽略屏蔽值(或NaN
)?
我希望它不是性能优化,否则您可以在数字化 function 之后进行屏蔽:
import numpy as np
A = np.arange(10,dtype=np.float)
A[0] = np.nan
A[-1] = np.nan
bins = np.array([1,2,7])
res = np.digitize(A,bins)
# here np.nan is assigned to the highes bin
# using numpy '1.17.2'
print(res)
# sp you mask you array after the execution of
# np.digitize
print(res[~np.isnan(A)])
>>> [3 1 2 2 2 2 2 3 3 3]
>>> [1 2 2 2 2 2 3 3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.