一次查找和显示N天的线性回归曲线

问题描述 投票:0回答:1

想要每n天找到线性回归线,这通常称为“线性回归曲线”。

我有时间序列数据,其中我需要每n天找到线性回归曲线 - 基本上每n天的斜率将发生变化,并且每n天会有一条新的线性回归线 - 所有这些都在以后连接在图表上。

def slope_intercept(x_val, y_val):
x = np.array(x_val)
y = np.array(y_val)
m = ( ( (np.mean(x)*np.mean(y) ) - np.mean(x*y)) /
    ( ( np.mean(x)*np.mean(x)) - np.mean(x*x)))

m = round(m,2)
b=(np.mean(y)-np.mean(x)*m)
b=round(b,2)
return m,b

m,b=slope_intercept(future.index.tolist(), future['close'].tolist())
future['reg_line'] = [(m*x)+b for x in future.index.tolist()]

一旦找到,我想在图表上将其可视化。下面是所述可视化的图片,以及用于整个数据时间的线性回归的代码的示例。我猜它是某种类型的for循环来做到这一点? Picture of the linear-regression-curve I am trying to make, disregard the candlesticks

python pandas linear-regression curve-fitting
1个回答
0
投票
from scipy.stats import linregress

linregress(x,y)

x和y是数组或列表。

© www.soinside.com 2019 - 2024. All rights reserved.