尝试使用两个scipy.stats.pearsonr
列表运行Decimal
会让scipy
感到不快:
print type(signals)
print type(signals[0])
print type(prices)
print type(prices[0])
<type 'list'>
<class 'decimal.Decimal'>
<type 'list'>
<class 'decimal.Decimal'>
correlation = stats.pearsonr(signals, prices)
Traceback (most recent call last):
File "commod.py", line 74, in <module>
main()
File "commod.py", line 69, in main
correlation = stats.pearsonr(signals, prices)
File "/home/.../venv/local/lib/python2.7/site-packages/scipy/stats/stats.py", line 2445, in pearsonr
t_squared = r*r * (df / ((1.0 - r) * (1.0 + r)))
TypeError: unsupported operand type(s) for -: 'float' and 'Decimal'
有人为此遇到了解决方案吗?
答案 0 :(得分:0)
正如错误所示,问题出现是因为pearsonr
函数试图对float和Decimal运行算术运算,而python不喜欢这样。但是,您可以将十进制列表转换为浮点数 -
stats.pearsonr([float(val) for val in signals], [float(val) for val in prices])
即使您在评论中提到您想要使用Decimal来获得精确格式,浮点错误也很难改变您的结果。