是否有人有一段代码来计算熊猫数据框的滚动线性回归曲线?
我已经有了计算整个数据帧的斜率的函数,但我要注意如何应用/使用它:
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(df1.index.tolist(), df1['Close'].tolist())
df1['reg_line'] = [(m*x)+b for x in df1.index.tolist()]
谢谢和欢呼,
即