我正在尝试计算特定时间范围内股票价格开发的线性回归。代码运行良好,直到我添加stats.linregress()函数;给我以下错误:线性回归函数运行到“nan未定义”错误
Traceback (most recent call last):
File "C:/[...]/PycharmProjects/Portfolio_Algorithm/Main.py", line 3, in
from scipy import stats
File "C:[...]\Continuum\Anaconda3\lib\site-packages\scipy__init__.py", line 61, in
from numpy import show_config as show_numpy_config
File "C:[...]\Python\Python35\site-packages\numpy__init__.py",line 142, in
from . import add_newdocs
File "C:[...]\Python\Python35\site-packages\numpy\add_newdocs.py",line 13, in
from numpy.lib import add_newdoc
File "C:[...]\Python\Python35\site-packages\numpy\lib__init__.py",line 8, in
from .type_check import *
File "C:[...]\Python\Python35\site-packages\numpy\lib\type_check.py", line 11, in
import numpy.core.numeric as _nx
File "C:[...]\Python\Python35\site-packages\numpy\core__init__.py", line 21, in
from . import umath
File "C:[...]\Python\Python35\site-packages\numpy\core\umath.py",line 30, in
NAN = nan NameError: name 'nan' is not defined
我使用Python 3.5,森蚺(用于SciPy的和numpy的)和PyCharm。
from yahoo_finance import Share
from math import log
from scipy import stats
yahoo = Share('YHOO')
date_list=[]
price_list=[]
timeframe = (yahoo.get_historical('2016-01-01', '2016-10-29'))
for item in timeframe:
date_list.extend([item['Date']])
price_list.extend([log(float(item['Close']))])
slope = stats.linregress(date_list, price_list)
print(slope)
当我运行scipy用户指南的例子时,我得到了同样的错误。 例(link):
from scipy import stats
np.random.seed(12345678)
x = np.random.random(10)
y = np.random.random(10)
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
print("r-squared:", r_value**2)
有谁知道什么可能导致这个错误吗?
看起来像一些数字没有填写或东西,所以价格列为南(不是数字)。 'linregress'大概只期望数字,所以会引发错误。你将不得不看看'Share()'返回的是什么,如果它返回的不是数字的东西,你必须在倒退之前处理它。 – Iluvatar
请显示'umath.py'的更多路径。这将有助于了解哪个软件包正在生成错误。 –
事实上,它将有助于查看* complete *错误消息(即完整的回溯)。复制并粘贴到问题中。 –