python线性回归预测股票走势
提前说明:虽然最后预测股价和真实价之间有差距,但涨跌的趋势大致相同。而且在预测时没有考虑到涨跌停的因素,所以预测结果的涨跌幅度比真实数据要大。
本人上传只作学习练习笔记作用,并不打算用在实操的
直接上代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import math
#线性回归
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
#使用如下方式下载某个公司的股票交易历史数据
#比如 000001 平安银行
#使用pip install tushare 安装tushare python包
import tushare as ts
data =ts.get_hist_data(600519)
data.to_csv(./600519.csv)
#导入数据,输出前几行看一下情况
data=pd.read_csv(./600519.csv)
df = data[[open,high,low,volume,close]]
# 划分特征值和目标值
featureDatas = df[[open,high,low,volume]]
feature = featureDatas.values
target = np.array(df[close])
# 划分训练集和测试集
feature_train, feature_test, target_train, target_test = train_test_split(feature,target,test_size = 0.05)
lrtoot = LinearRegression() # 创建线性回归对象
lrtoot.fit(feature_train,target_train) # 训练
# 用测试集预测结果
predictByTest = lrtoot.predict(feature_test)
predictDays = int(math.ceil(0.05 * len(df))) # 预测的天数
# 在前95%的交易日中,设置预测结果和收盘价一致
index = 0
while index < len(data) - predictDays:
df.loc[index,predictValue] = data.loc[index,close] # 把训练集部分的预测股价设置成收盘价
df.loc[index,date] = data.loc[index,date] # 训练集部分的日期
index = index + 1
# 在后5%的交易日中,用测试集推算预测股价
predictedCnt = 0
while predictedCnt < predictDays:
df.loc[index,predictValue] = predictByTest[predictedCnt] # 把df中表示测试结果的predictedVal列设置成相应的预测结果
df.loc[index,date] = data.loc[index,date] # 逐行设置了每条记录中的日期
predictedCnt =predictedCnt + 1
index =index + 1
将其可视化:
plt.figure(figsize=(30,10)) df[predictValue].plot(color=red,label=predict data,fontsize=30) df[close].plot(color=blue,label=real data,fontsize=30) plt.legend(loc = best,fontsize=40) # 绘制图例 # 设置x坐标的标签 major_index = df.index[df.index%30==0] major_xtics = df[date][df.index%30==0] plt.xticks(major_index,major_xtics) plt.setp(plt.gca().get_xticklabels(), rotation=30) # 带网格线,且设置了网格样式 plt.grid(linestyle=-.) plt.show()
