且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NumPy/SciPy中的广义累积函数?

更新时间:2023-02-26 15:41:23

NumPy的ufunc具有

NumPy's ufuncs have accumulate():

In [22]: np.multiply.accumulate([[1, 2, 3], [4, 5, 6]], axis=1)
Out[22]: 
array([[  1,   2,   6],
       [  4,  20, 120]])

不幸的是,在frompyfunc()版本的Python函数上调用accumulate()失败,并出现一个奇怪的错误:

Unfortunately, calling accumulate() on a frompyfunc()'ed Python function fails with a strange error:

In [32]: uadd = np.frompyfunc(lambda x, y: x + y, 2, 1)

In [33]: uadd.accumulate([1, 2, 3])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

ValueError: could not find a matching type for <lambda> (vectorized).accumulate, 
            requested type has type code 'l'

这是将NumPy 1.6.1与Python 2.7.3结合使用.

This is using NumPy 1.6.1 with Python 2.7.3.