简体   繁体   English

在numpy数组中有效地查找numpy数组中的值

[英]Find values in numpy array space-efficiently

I am trying to create a copy of my numpy array that contains only certain values. 我正在尝试创建仅包含特定值的numpy数组的副本。 This is the code I was using: 这是我使用的代码:

A = np.array([[1,2,3],[4,5,6],[7,8,9]])
query_val = 5
B = (A == query_val) * np.array(query_val, dtype=np.uint16)

... which does exactly what I want. ......这正是我想要的。

Now, I'd like query_val to be more than just one value. 现在,我希望query_val不仅仅是一个值。 The answer here: Numpy where function multiple conditions suggests using a logical and operation, but that's very space inefficient because you use == several times, creating multiple intermediate results. 答案在这里: Numpy函数多个条件建议使用逻辑和操作,但这是非常低效的,因为你使用==几次,创建多个中间结果。

In my case, that means I don't have enough RAM to do it. 在我的情况下,这意味着我没有足够的RAM来做到这一点。 Is there a way to do this properly in native numpy with minimal space overhead? 有没有办法在原生numpy中以最小的空间开销正确地执行此操作?

Here's one approach using np.searchsorted - 这是使用np.searchsorted的一种方法 -

def mask_in(a, b):
    idx = np.searchsorted(b,a)
    idx[idx==b.size] = 0
    return np.where(b[idx]==a, a,0)

Sample run - 样品运行 -

In [356]: a
Out[356]: 
array([[5, 1, 4],
       [4, 5, 6],
       [2, 4, 9]])

In [357]: b
Out[357]: array([2, 4, 5])

In [358]: mask_in(a,b)
Out[358]: 
array([[5, 0, 4],
       [4, 5, 0],
       [2, 4, 0]])

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

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