TA-Lib numpy “AssertionError: real is not double”

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

I have AssertionError using TA-Lib wrapper in python. Could you take a look at my code? I really appreciate your help.

import numpy as np import talib  #This works test_data = np.random.random(5) np_out = talib.SMA(test_data,3) print np_out  #How come this does not work?  I need to fix real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0] np_real_data = np.array(real_data,dtype=np.object) np_out = talib.SMA(np_real_data,3) print np_out 

error message:

  File "func.pyx", line 9200, in talib.func.SMA (talib/func.c:85610) AssertionError: real is not double 

I suspet the solution might be to convert double to real. I want to test that idea. How do I convert the real_data from double to real?

Thanks.

回答1:

I suspect the solution might be to convert double to real.

No. You have real data. TA-lib doesn't like "real data". You want to convert it to double float data.

re: qcc's unexplained answer:

f8 is a 64 bit "double precision" floating point number. http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

TA-lib wants numpy arrays of "double" floats as inputs.

http://en.wikipedia.org/wiki/Double-precision_floating-point_format

There are several methods you can use to get where you're going, probably the best for your purposes is:

    real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]     float_data = [float(x) for x in real_data]     np_float_data = np.array(float_data)     np_out = talib.SMA(np_float_data,3) 

Here are several others:

1)

    real_data = [float(x) for x in [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]]     np_real_data = np.array(real_data)     np_out = talib.SMA(np_real_data,3) 

2)

    real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]     np_real_data = np.array(real_data, dtype='f8')     np_out = talib.SMA(np_real_data,3) 

3)

    real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]     np_real_data = np.array(real_data, dtype=float)     np_out = talib.SMA(np_real_data,3) 

4)

    real_data = map(float, [135.01, 133.0, 134.0, 131.0, 133.0, 131.0])     np_real_data = np.array(real_data)     np_out = talib.SMA(np_real_data,3) 

5)

    real_data = [float(135.01), float(133.0), float(134.0), float(131.0),                   float(133.0), float(131.0)]     np_real_data = np.array(real_data)     np_out = talib.SMA(np_real_data,3) 


回答2:

try this

np_real_data = np.array(real_data,dtype='f8') 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!