2016-12-05 126 views
0

我正在尝试计算特定时间范围内股票价格开发的线性回归。代码运行良好,直到我添加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) 

有谁知道什么可能导致这个错误吗?

+0

看起来像一些数字没有填写或东西,所以价格列为南(不是数字)。 'linregress'大概只期望数字,所以会引发错误。你将不得不看看'Share()'返回的是什么,如果它返回的不是数字的东西,你必须在倒退之前处理它。 – Iluvatar

+0

请显示'umath.py'的更多路径。这将有助于了解哪个软件包正在生成错误。 –

+0

事实上,它将有助于查看* complete *错误消息(即完整的回溯)。复制并粘贴到问题中。 –

回答

0

这里是你的榜样,重新写入解决的几个问题:

from yahoo_finance import Share 
from math import log 
from scipy import stats 
from time import mktime, strptime 
import numpy as np 

yahoo = Share('YHOO') 
timeframe = yahoo.get_historical('2016-01-01', '2016-10-29') 
tpattern = '%Y-%m-%d' # Time-match-pattern 

dates = np.zeros(len(timeframe)) 
prices = np.zeros(len(timeframe)) 

for ii,item in enumerate(timeframe): 
    dates[ii] = mktime(strptime(item['Date'], tpattern)) 
    prices[ii] = float(item['Close']) 

slope = stats.linregress(dates, np.log10(prices)) 
print(slope) 

get_historical方法返回的dict,每个包含字符串列表。您需要将您的数据转换为float以使其有用。这似乎是你的例子中的主要问题。

由于您在开始时拉取数据并知道要分析多少个数据点,因此没有理由将列表用作数据结构; numpy数组效率更高。因此,使用datesprices而不是列表。

使用numpy数组,对整个价格数据阵列进行操作以生成对数而不是在循环中一次处理效率更高。

您可能打算以10为底的对数,而不是斜率的自然对数。

+0

@dwilfling:奇怪。我使用Python2和Python 3获得了相同的结果(除了在Python2中使用print语句)。此外,我在Windows和Ubuntu 16.04上获得了相同的结果:LinregressResult(斜率= 6.8938424929225118e-09,截距= -8.5328940373602329,rvalue = 0.92426168485508664,pvalue = 1.61357088610067e-88,stderr = 1.9791154092032521e-10) –

+0

我刚刚意识到你编辑的问题包括完整的回溯,并且该示例不运行。这表明您的安装有问题。我建议你删除并重新安装Python3。 –