python - 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)
解决方案
推荐阅读
- python - 如何计算 Django 的总工作时间?
- excel - VBA隐藏/取消隐藏活动单元格下方的3行
- c# - 名称“locBox 不存在”
- regex - 如何使用 Scala 从损坏的字符串数据中提取好的 JSON?
- ffmpeg - 排查 ffmpeg/ffplay 客户端 RTSP RTP UDP * 多播 * 问题
- java - 多线程异常:“非法尝试将集合与两个打开的会话相关联”?
- python - 在列数据框中获取句子结构
- mysql - Flutter 如何格式化从数据库中拉取的日期和时间
- docker - 如何在 Jenkins 容器化 docker 环境中运行 liquibase docker 镜像
- c# - 我试图制作一个左右移动的 obstical,但它什么也没做。我使用统一 5