繁体   English   中英

在numpy中计算超过阈值的数组值的最快方法

[英]Fastest way to count array values above a threshold in numpy

我有一个包含10 ^ 8个浮点的numpy数组,并想计算其中有多少个> =给定阈值。 速度至关重要,因为必须对大量此类阵列执行操作。 到目前为止的参赛者是

np.sum(myarray >= thresh)

np.size(np.where(np.reshape(myarray,-1) >= thresh))

计数矩阵中大于一个值的所有值的答案表明np.where()会更快,但是我发现时序结果不一致。 我的意思是,对于某些实现和布尔条件,np.size(np.where(cond))比np.sum(cond)快,但对于某些情况,则要慢一些。

具体来说,如果大部分条目满足条件,则np.sum(cond)会明显更快,但是如果很小一部分(可能小于十分之一),则np.size(np.where(cond))会获胜。

问题分为两部分:

  • 还有其他建议吗?
  • np.size(np.where(cond))花费的时间随着cond为真的条目数而增加是否有意义?

使用cython可能是一个不错的选择。

import numpy as np
cimport numpy as np
cimport cython
from cython.parallel import prange


DTYPE_f64 = np.float64
ctypedef np.float64_t DTYPE_f64_t


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
cdef int count_above_cython(DTYPE_f64_t [:] arr_view, DTYPE_f64_t thresh) nogil:

    cdef int length, i, total
    total = 0
    length = arr_view.shape[0]

    for i in prange(length):
        if arr_view[i] >= thresh:
            total += 1

    return total


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def count_above(np.ndarray arr, DTYPE_f64_t thresh):

    cdef DTYPE_f64_t [:] arr_view = arr.ravel()
    cdef int total

    with nogil:
       total =  count_above_cython(arr_view, thresh)
    return total

建议的不同方法的时间。

myarr = np.random.random((1000,1000))
thresh = 0.33

In [6]: %timeit count_above(myarr, thresh)
1000 loops, best of 3: 693 µs per loop

In [9]: %timeit np.count_nonzero(myarr >= thresh)
100 loops, best of 3: 4.45 ms per loop

In [11]: %timeit np.sum(myarr >= thresh)
100 loops, best of 3: 4.86 ms per loop

In [12]: %timeit np.size(np.where(np.reshape(myarr,-1) >= thresh))
10 loops, best of 3: 61.6 ms per loop

使用更大的数组:

In [13]: myarr = np.random.random(10**8)

In [14]: %timeit count_above(myarr, thresh)
10 loops, best of 3: 63.4 ms per loop

In [15]: %timeit np.count_nonzero(myarr >= thresh)
1 loops, best of 3: 473 ms per loop

In [16]: %timeit np.sum(myarr >= thresh)
1 loops, best of 3: 511 ms per loop

In [17]: %timeit np.size(np.where(np.reshape(myarr,-1) >= thresh))
1 loops, best of 3: 6.07 s per loop

暂无
暂无

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

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