首页 > 解决方案 > Numba 函数抛出错误

问题描述

我正在尝试计算归一化的日内强度计算并使用 numba 来加速循环。

我不断收到以下错误:

Traceback (most recent call last):

  File "<ipython-input-70-228319e17353>", line 1, in <module>
    df['price_intensity'] = rh.price_intenstiy(h = df['high'],

  File "C:\Users\Jordan Howell\anaconda3\lib\site-packages\numba\core\dispatcher.py", line 415, in _compile_for_args
    error_rewrite(e, 'typing')

  File "C:\Users\Jordan Howell\anaconda3\lib\site-packages\numba\core\dispatcher.py", line 358, in error_rewrite
    reraise(type(e), e, None)

  File "C:\Users\Jordan Howell\anaconda3\lib\site-packages\numba\core\utils.py", line 80, in reraise
    raise value.with_traceback(tb)

TypingError: cannot determine Numba type of <class 'scipy.stats._continuous_distns.norm_gen'>

我不确定是pandas列扔它还是什么。我已经检查过https://numba.pydata.org/numba-doc/dev/reference/numpysupported.html。我认为我对文档很好,因为我将熊猫系列转换为numpy数组。

我对 numba 不太熟悉,但这是我到目前为止所拥有的:

import numba as nb
@nb.njit()
def price_intenstiy(h,l,c, o, window=15,smoothing = 2):
    """
    

    Parameters
    ----------
    h = high
    l = low
    c = adjusted close
    o =open
    window = lookback window
    smoothing : parameter for smoothing. usually 2/a-2

    Returns
    -------
    None.

    """
    h = np.array(h)
    l = np.array(l)
    c = np.array(c)
    o = np.array(o)
    
    output = nb.typed.List()
    
    denom = h[0] - l[0]
    
    if denom < 1e-60:
        denom = 1e-60
    else:
        denom = denom
        
    output.append((c[0]-o[0])/denom)
    
    i = 1
    while i < len(h):
        denom = h[i]-l[i]
        high_low = (h[i] - l[i])
        high_previous = (h[i] - c[i-1])
        previous_low = (c[i-1] - l[i])
        term = np.max((high_low, high_previous,
                                previous_low))
        output.append(term)
        
    
    smoothed_output = nb.typed.List()
    alpha = 2/(smoothing+1)
    smoothed = output[0]
    r = 1
    while r < len(output):
        smoothed = alpha*output[r]+(1-alpha)*smoothed
        smoothed_output.append(smoothed)
        
    p = 0
    while p < len(smoothed_output):
        smoothed_output[p] = 100*norm.cdf(.8*np.sqrt(window)*smoothed_output[p])-50
        
    return np.array(smoothed_output)
df['price_intensity'] = rh.price_intenstiy(h = df['high'],
                                           l = df['low'],
                                           c = df['adjusted_close'], 
                                           o = df['open'], 
                                           window=15,
                                           smoothing = 2)

标签: pythonnumba

解决方案


推荐阅读