微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当增加python中的数据数量时,linregress返回nan

如何解决当增加python中的数据数量时,linregress返回nan

我每天尝试根据股票价格创建方程,这是我的代码

你可以跳到最后 # 段,我认为这是问题

# Import  
import yfinance as yf
import numpy as np
from collections import Counter
from datetime import date

# stock list
stock_list = ['EGCO.BK','KBANK.BK','KTB.BK','LH.BK','MINT.BK','TTW.BK']


# Get the data 
data = yf.download(stock_list,'2010-01-01',str(date.today()))

# create x and y value ; x = date count,y = stock price
data.len = len(data)
x = list(range(1,data.len+1))
Adj_close = data['Adj Close']

#create list of stock price
y = Adj_close.T.values.tolist()

#find equation for each stock
from scipy.stats import linregress
from scipy import stats
equation = []
Slope = []
Intercept = []
for num in y :
    equation.append(linregress(x,num))
    
    #find slope and intercept for cal return
    slope,intercept,r_value,p_value,std_err = stats.linregress(x,num)
    Stat = (slope,std_err)
    Slope.append(Stat[0])
    Intercept.append(Stat[1])
print(equation)

当它打印slope的值时,截距,其余都是nan一样

/home/nitipong/.local/lib/python3.8/site-packages/scipy/stats/_distn_infrastructure.py:1932: RuntimeWarning: invalid value encountered in less_equal
  cond2 = cond0 & (x <= _a)
[LinregressResult(slope=nan,intercept=nan,rvalue=nan,pvalue=nan,stderr=nan),LinregressResult(slope=nan,stderr=nan)]

但是当我将数据从日期 '2010-01-01' 调整为 '2020-01-01'

它打印没有nan的所有值

为什么会这样或者数据数量有限制?

但我尽量减少库存。好像有些库存有问题

样本(日期为 2010 年)

with stock A,B   it return  (ok value,ok value)

with stock A,C   it return  (ok value,nan)

with stock A,C   it return  (nan,nan)

有人能帮我解决这个困惑吗?

#python 3.8.5 版

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。