首页  

沪深300 线性回归画趋势线     所属分类 quant 浏览量 286
沪深300 线性回归画趋势线趋势线



沪深300 最近的收盘数据


20230810 3975.717
20230811 3884.254
20230814 3855.906
20230815 3846.536
20230816 3818.334
20230817 3831.101
20230818 3784.00
20230821 3729.56
20230822 3758.23
20230823 3696.63
20230824 3723.43
20230825 3709.15 

import matplotlib.pyplot as plt 
import numpy as np 

y = [3818.334,3831.101,3784.00,3729.56,3758.23,3696.63,3723.43,3709.15]

x = range(1,len(y)+1)

# y = np.array([3784.00,3729.56,3758.23,3696.63,3723.43,3709.15])
 
coefficients = np.polyfit(x, y, deg=1)

# y2 = x*coefficients[0] + coefficients[1]

y2 = x*coefficients[0] + coefficients[1]

plt.scatter(x, y, c='r') 
plt.plot(x, y2, 'b') 
plt.show() 


读取本地数据 线性回归拟合并画图 import matplotlib.pyplot as plt import numpy as np hs300file = "/data/hs300.txt" hs300df = pd.read_csv(hs300file) y = hs300df[hs300df["time"]>='2023-01-01'][["close"]] x = range(1,len(y)+1) coefficients = np.polyfit(x, y, deg=1) y2 = x*coefficients[0] + coefficients[1] plt.scatter(x, y, c='r') plt.plot(x, y2, 'b') plt.show()

上一篇     下一篇
numpy polyfit 多项式拟合

沪深300指数和沪深300ETF 对比分析

时序数据趋势判断

线性回归中常见的一些统计学术语(RSE RSS TSS ESS MSE RMSE R2 Pearson's r)

线性回归的R方

简单线性回归 斜率 截距 计算