Numba不识别numpy.maximum.accumulate(),如何修改以下代码?

Yun*_*Wei 8 python-2.7 numba

错误消息:TypingError:在 nopython 模式管道中失败(步骤:nopython 前端) Function() 类型的未知属性“accumulate”。

下面的代码如何修改?谢谢。

import numba
import numpy as np


@numba.jit(nopython=True)
def maxdd(x):
    temp = np.maximum.accumulate(x) - x
    ide = len(x) - np.argmax(temp[::-1]) - 1
    ids = np.argmax(x[:ide])
    mdd = x[ide] - x[ids]
    ide += 1
    return mdd, ids, ide
Run Code Online (Sandbox Code Playgroud)

Ana*_*eev 4

我尝试使用矢量化 ufunc 以这种方式计算运行最大值:

@numba.vectorize(["int32(int32,int32)","int64(int64,int64)","float32(float32,float32)"])
@numba.njit()
def nmax(x, y):
    if x>y:
        return x
    else:
        return y
@numba.njit()
def test():
    a = np.arange(12)[::-1]
    print(a)
    return nmax.accumulate(a)
test()
Run Code Online (Sandbox Code Playgroud)

但是,在 Numba 0.46.0 中,这会引发错误

Function(numba._DUFunc 'nmax') 类型的未知属性 'accumulate'

目前看来 Numba 无法在 nopython 模式下使用向量化函数(?),不幸的是,导致 un-njiting 测试给出了预期的结果

[11 10 9 8 7 6 5 4 3 2 1 0]

数组([11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11])

现在我们必须手动编写这样的函数

@numba.njit()
def running_max(x):
    rmax=x[0]
    y=np.empty_like(x)
    for i,val in enumerate(x):
        if val>rmax: rmax=val
        y[i]=rmax
    return y
Run Code Online (Sandbox Code Playgroud)

我已向 Numba 团队提出了问题。