【问题标题】:Masked values in numpy digitizenumpy digitize 中的屏蔽值
【发布时间】:2019-11-19 11:50:12
【问题描述】:

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

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

尽管如此,np.digitize 将掩码值抛出为-1。有没有其他方法可以让np.digitize 忽略屏蔽值(或NaN)?

【问题讨论】:

  • 是一维数组吗?
  • @some_name.py 是

标签: python numpy nan masked-array


【解决方案1】:

我希望它不是性能优化,否则你可以 数字化功能后的掩码:

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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-05
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多