2016-12-05 125 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。 –