0

我希望numpy digitize忽略我数组中的一些值。为了实现这一点,我将不需要的值替换为NaN并掩盖了这些NaN值:

import numpy as np
A = np.ma.array(A, mask=np.isnan(A))

尽管如此np.digitize ,将掩码值抛出为-1. 是否有替代方法可以np.digitize忽略屏蔽值(或NaN)?

4

1 回答 1

2

我希望它不是性能优化,否则您可以在 digitize 函数之后进行屏蔽:

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]
于 2019-11-19T16:10:07.653 回答