快速Numpy滚动_产品 [英] Fast numpy rolling_product
问题描述
我需要rolling_product函数或expanding_product函数.
I need a rolling_product function, or an expanding_product function.
有各种pandas
rolling_XXXX
和expanding_XXXX
函数,但是我惊讶地发现缺少expanding_product()
函数.
There are various pandas
rolling_XXXX
and expanding_XXXX
functions, but I was surprised to discover the absence of an expanding_product()
function.
为了让事情正常进行,我一直在使用这种相当慢的替代方法
To get things working I've been using this rather slow alternative
pd.expanding_apply(temp_col, lambda x : x.prod())
我的数组通常包含32,000个元素,因此这被证明是一个瓶颈.我很想尝试log()
,cumsum()
和exp()
,但是我认为我应该在这里提出问题,因为可能会有更好的解决方案.
My arrays often have 32,000 elements so this is proving to be a bit of a bottleneck. I was tempted to try log()
, cumsum()
, and exp()
, but I thought I should ask on here since there might be a much better solution.
推荐答案
我有一个更快的机制,尽管您需要运行一些测试以查看准确性是否足够.
I have a faster mechanism, though you'll need to run some tests to see if the accuracy is sufficient.
这是原始的exp/sum/log版本:
Here's the original exp/sum/log version:
def rolling_prod1(xs, n):
return np.exp(pd.rolling_sum(np.log(xs), n))
这是一个使用累积乘积,将其转移(用nans预先填充),然后将其重新分配出去的版本.
And here's a version that takes the cumulative product, shifts it over (pre-filling with nans), and then divides it back out.
def rolling_prod2(xs, n):
cxs = np.cumprod(xs)
nans = np.empty(n)
nans[:] = np.nan
nans[n-1] = 1.
a = np.concatenate((nans, cxs[:len(cxs)-n]))
return cxs / a
在此示例中,两个函数均返回相同的结果:
Both functions return the same result for this example:
In [9]: xs
Out[9]: array([ 1., 2., 3., 4., 5., 6., 7., 8., 9.])
In [10]: rolling_prod1(xs, 3)
Out[10]: array([ nan, nan, 6., 24., 60., 120., 210., 336., 504.])
In [11]: rolling_prod2(xs, 3)
Out[11]: array([ nan, nan, 6., 24., 60., 120., 210., 336., 504.])
但是第二个版本要快得多:
But the second version is much faster:
In [12]: temp_col = np.random.rand(30000)
In [13]: %timeit rolling_prod1(temp_col, 3)
1000 loops, best of 3: 694 µs per loop
In [14]: %timeit rolling_prod2(temp_col, 3)
10000 loops, best of 3: 162 µs per loop
这篇关于快速Numpy滚动_产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!