1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python:一元线性回归分析实例:时间序列分段

python:一元线性回归分析实例:时间序列分段

时间:2023-03-13 01:05:12

相关推荐

python:一元线性回归分析实例:时间序列分段

一元线性回归分析实例:时间序列分段

以沪深300指数基金净值为例

基金净值数据格式:date,jz,ljjz

-01-02,1.0194,1.0194

-01-03,1.0177,1.0177

linear_mod_2.py

# coding=utf-8import os, sysimport numpy as npimport matplotlib.pyplot as pltimport pandas as pdfrom sklearn.linear_model import LinearRegression# python一元线性回归分析实例:指数基金净值if len(sys.argv) ==2:fcode = sys.argv[1]else:print('usage: python linear_mod_2.py fcode ')sys.exit(1)if len(fcode) !=6:print(' fcode is char(6)')sys.exit(2)file1 = "./" +fcode +'.csv'if not os.path.exists(file1):print(file1 +' is not exists.')sys.exit(3)# 用pandas读取csvdf = pd.read_csv(file1)df = df[ df['date'] > '-01-01']df.index = pd.to_datetime(df.date)y = df['jz'].values # 基金净值x = np.arange(0,len(y),1)# 构造X列表和Y列表,reshape(-1,1)改变数组形状,为只有一个属性x = x.reshape(-1,1)y = y.reshape(-1,1)# 时间序列分段1df1 = df[ df['date'] < '-04-20']y1 = df1['jz'].values # 基金净值x1 = np.arange(0,len(y1),1)x1 = x1.reshape(-1,1)y1 = y1.reshape(-1,1)begin = len(y1)# 时间序列分段2dates = pd.date_range('-04-20','-06-09')df2 = df[ df.index.isin(dates.values)]y2 = df2['jz'].values # 基金净值x2 = np.arange(begin, begin+len(y2),1)x2 = x2.reshape(-1,1)y2 = y2.reshape(-1,1)begin = begin+len(y2)# 时间序列分段3dates = pd.date_range('-06-10','-01-24')df3 = df[ df.index.isin(dates.values)]y3 = df3['jz'].values # 基金净值x3 = np.arange(begin, begin+len(y3),1)x3 = x3.reshape(-1,1)y3 = y3.reshape(-1,1)begin = begin+len(y3)# 时间序列分段4dates = pd.date_range('-02-03','-03-05')df4 = df[ df.index.isin(dates.values)]y4 = df4['jz'].values # 基金净值x4 = np.arange(begin, begin+len(y4),1)x4 = x4.reshape(-1,1)y4 = y4.reshape(-1,1)begin = begin+len(y4)# 时间序列分段5dates = pd.date_range('-03-06','-03-20')df5 = df[ df.index.isin(dates.values)]y5 = df5['jz'].values # 基金净值x5 = np.arange(begin, begin+len(y5),1)x5 = x5.reshape(-1,1)y5 = y5.reshape(-1,1)begin = begin+len(y5)# 时间序列分段6dates = pd.date_range('-03-21','-07-11')df6 = df[ df.index.isin(dates.values)]y6 = df6['jz'].values # 基金净值x6 = np.arange(begin, begin+len(y6),1)x6 = x6.reshape(-1,1)y6 = y6.reshape(-1,1)begin = begin+len(y6)# 时间序列分段7df7 = df[ df['date'] > '-07-11']y7 = df7['jz'].values # 基金净值x7 = np.arange(begin, begin+len(y7),1)x7 = x7.reshape(-1,1)y7 = y7.reshape(-1,1)begin = begin+len(y7)# 构造回归对象model = LinearRegression()model.fit(x1, y1)Y1 = model.predict(x1) # 获取预测值model.fit(x2, y2)Y2 = model.predict(x2)model.fit(x3, y3)Y3 = model.predict(x3)model.fit(x4, y4)Y4 = model.predict(x4)model.fit(x5, y5)Y5 = model.predict(x5)model.fit(x6, y6)Y6 = model.predict(x6)model.fit(x7, y7)Y7 = model.predict(x7)# 构造返回字典predictions = {}predictions['intercept'] = model.intercept_ # 截距值predictions['coefficient'] = model.coef_ # 回归系数(斜率值)#predictions['predict_value'] = Y7 print(predictions)# 绘图fig, ax = plt.subplots(figsize=(10,6))# 绘出已知数据散点图#plt.scatter(x, y, color ='blue')# 绘曲线图ax.plot(x, y, '-', label='jz') # 基金净值# 绘出预测直线ax.plot(x1, Y1, 'r--.', label='fit1')ax.plot(x2, Y2, 'g--.', label='fit2')ax.plot(x3, Y3, 'r--.', label='fit3')ax.plot(x4, Y4, 'r--.', label='fit4')ax.plot(x5, Y5, 'g--.', label='fit5')ax.plot(x6, Y6, 'r--.', label='fit6')ax.plot(x7, Y7, 'y--.', label='fit7')ax.legend(loc='upper left')plt.title('predict fund net value: ' +fcode)plt.xlabel('x')plt.ylabel('jz')plt.grid()plt.show()

以沪深300指数基金净值为例

运行 pythonlinear_mod_2.py 660008

python tushare 读取股票数据并存盘

以股票 000063 中兴通讯为例

运行 python stock1.py 000063

将 'jz' 全替换为 'close' 就可以为股票收盘价 做一元线性回归分析

运行 python linear_mod_2.py 000063

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。