如果 numpy 数组元素高于特定阈值,则将它们设置为零 [英] Set numpy array elements to zero if they are above a specific threshold

查看:110
本文介绍了如果 numpy 数组元素高于特定阈值,则将它们设置为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

因为我目前使用的是 for 循环,它非常慢:

Because I currently use a for loop, which is very slow:

# 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

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

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

推荐答案

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

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

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

但是,正如@unutbu 正确指出,numpy 允许列表索引,并且元素比较为您提供索引列表,所以:

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

super_threshold_indices = a > thresh
a[super_threshold_indices] = 0

会更快.

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

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 数组元素高于特定阈值,则将它们设置为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆