【问题标题】:Linear regression function runs into "nan is not defined" error线性回归函数遇到“nan is not defined”错误
【发布时间】: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 的日期。我需要将日期转换为数字/浮点数吗?

标签: python numpy scipy nan


【解决方案1】:

这是您的示例,已重新编写以解决一些问题:

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 为底的对数,而不是斜率的自然对数。

【讨论】:

  • @dwilfling:奇怪。我使用 Python2 和 Python 3 得到了相同的结果(除了在 Python2 中使用 print 语句)。此外,我在Windows和Ubuntu 16.04上获得了相同的结果
  • 我刚刚意识到您编辑了问题以包含完整的回溯,并且该示例没有运行。这表明您的安装有问题。我建议你删除并重新安装 Python3。
猜你喜欢
  • 2021-11-08
  • 2021-10-18
  • 2020-12-03
  • 2020-11-07
相关资源
最近更新 更多