【发布时间】:2017-04-20 04:43:58
【问题描述】:
我正在尝试计算特定时间范围内股票价格发展的线性回归。代码运行良好,直到我添加了 stats.linregress() 函数;给我以下错误:
回溯(最近一次通话最后一次):
文件 “C:/[...]/PycharmProjects/Portfolio_Algorithm/Main.py”,行 3、在from scipy import stats文件“C:[...]\Continuum\Anaconda3\lib\site-packages\scipy__init__.py”,第 61 行,在
from numpy import show_config as show_numpy_config文件“C:[...]\Python\Python35\site-packages\numpy__init__.py”,第 142 行,在
from . import add_newdocs文件“C:[...]\Python\Python35\site-packages\numpy\add_newdocs.py”,第 13 行,在
from numpy.lib import add_newdoc文件“C:[...]\Python\Python35\site-packages\numpy\lib__init__.py”,第 8 行,在
from .type_check import *文件“C:[...]\Python\Python35\site-packages\numpy\lib\type_check.py”,第 11 行,在
import numpy.core.numeric as _nx文件“C:[...]\Python\Python35\site-packages\numpy\core__init__.py”,第 21 行,在
from . import umath文件“C:[...]\Python\Python35\site-packages\numpy\core\umath.py”,第 30 行,在
NAN = nan NameError: name 'nan' 未定义
我正在使用 Python 3.5、Anaconda(用于 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)
有谁知道可能导致错误的原因?
【问题讨论】:
-
好像有些数字没有填写什么的,所以价格被列为nan(不是数字)。
linregress可能只需要数字,因此会引发错误。您将不得不查看Share()返回的内容,如果它也返回不是数字的内容,您必须在回归之前处理它。 -
请显示更多
umath.py的路径。这将有助于了解哪个包产生了错误。 -
事实上,查看 complete 错误消息(即完整的回溯)会有所帮助。将其复制并粘贴到问题中。
-
听起来您可能为您的一个文件选择了一个错误的名称,从而隐藏了其他一些模块。
-
嗨 - 感谢您的帮助。我已添加完整的错误消息。我也在使用 linregress 的日期。我需要将日期转换为数字/浮点数吗?