Python 如果 numpy 数组元素高于特定阈值,则将它们设置为零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28430904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:16:35  来源:igfitidea点击:

Set numpy array elements to zero if they are above a specific threshold

pythonarraysnumpy

提问by bluevoxel

Say, I have a numpy array consists of 10elements, for example:

说,我有一个由10元素组成的 numpy 数组,例如:

a = np.array([2, 23, 15, 7, 9, 11, 17, 19, 5, 3])

a = np.array([2, 23, 15, 7, 9, 11, 17, 19, 5, 3])

Now I want to efficiently set all avalues higher than 10to 0, so I'll get:

现在我想有效地将​​所有a值设置为高于10to 0,所以我会得到:

[2, 0, 0, 7, 9, 0, 0, 0, 5, 3]

[2, 0, 0, 7, 9, 0, 0, 0, 5, 3]

Because I currently use a forloop, which is very slow:

因为我目前使用的是一个for循环,速度很慢:

# Zero values below "threshold value".
def flat_values(sig, tv):
    """
    :param sig: signal.
    :param tv: threshold value.
    :return:
    """
    for i in np.arange(np.size(sig)):
        if sig[i] < tv:
            sig[i] = 0
    return sig

How can I achieve that in the most efficient way, having in mind big arrays of, say, 10^6elements?

我怎样才能以最有效的方式实现这一点,考虑到10^6元素的大数组?

采纳答案by Marcus Müller

Generally, list comprehensions are faster than forloops in python (because python knows that it doesn't need to care for a lot of things that might happen in a regular forloop):

通常,列表推导式比forpython 中的循环更快(因为 python 知道它不需要关心在常规for循环中可能发生的很多事情):

a = [0 if a_ > thresh else a_ for a_ in a]

but, as @unutbu correctly pointed out, numpy allows list indexing, and element-wise comparison giving you index lists, so:

但是,正如@unutbu 正确指出的那样,numpy 允许列表索引和元素比较为您提供索引列表,因此:

super_threshold_indices = a > thresh
a[super_threshold_indices] = 0

would be even faster.

会更快。

Generally, when applying methods on vectors of data, have a look at numpy.ufuncs, which often perform much better than python functions that you map using any native mechanism.

通常,在对数据向量应用方法时,请查看numpy.ufuncs,它的性能通常比使用任何本机机制映射的 Python 函数要好得多。

回答by unutbu

In [7]: a = np.array([2, 23, 15, 7, 9, 11, 17, 19, 5, 3])

In [8]: a[a > 10] = 0

In [9]: a
Out[9]: array([2, 0, 0, 7, 9, 0, 0, 0, 5, 3])

回答by yellow01

If you don't want to change your original array

如果您不想更改原始数组

In [1]: import numpy as np


In [2]: a = np.array([2, 23, 15, 7, 9, 11, 17, 19, 5, 3])


In [3]: b = a * (a <= 10)


In [4]: a

Out[4]: array([ 2, 23, 15,  7,  9, 11, 17, 19,  5,  3])


In [5]: b

Out[5]: array([2, 0, 0, 7, 9, 0, 0, 0, 5, 3])