构建LSTM模型预测上证指数股票涨跌

深度学习 2019-03-21

首先导入数据

数据我已经上传到我的github上了直接取下载即可:
https://github.com/ixxooi-baijian/data/blob/master/2019-03-21前的上证指数股票数据.xlsx

import pandas as pd
from pylab import mpl

df = pd.read_excel('2019-03-21前的上证指数股票数据.xlsx', index_col='Date')
# 指定默认字体,可显示中文
mpl.rcParams['font.sans-serif'] = ['FangSong'] 
# 解决保存图像是负号'-'显示为方块的问题
mpl.rcParams['axes.unicode_minus'] = False 
# 使得可在jupyter notebook中显示图片
%pylab inline

可视化关盘价格(线性图)

# 关盘价 线性图
df['Close'].plot('line', title='2019-03-21前的上证指数关盘价线性图', figsize=(12, 7))

请输入图片描述

将数据集划分训练数据集与测试数据集

training_set = df.iloc[0:2240-350, 3:4].values
test_set = df.iloc[2240-350:, 3:4].values

标准化数据集

from sklearn.preprocessing import MinMaxScaler
import numpy as np

# 将大数值数据化为 0,1间的数据,方便模型计算
MMsc = MinMaxScaler(feature_range=  (0, 1))
training_set_scaled = MMsc.fit_transform(training_set)

X_train = []
Y_train = []
for i in range(60, len(training_set)):
    # 参数0为将多维数组 打平 为一维
    X_train.append(training_set_scaled[i-60:i, 0])
    Y_train.append(training_set_scaled[i, 0])
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))

构建LSTM神经网络

from keras.callbacks import ModelCheckpoint
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout

regressor = Sequential()

regressor.add(LSTM(units = 35, return_sequences = True, input_shape = (X_train.shape[1], 1)))
regressor.add(Dropout(0.14))

regressor.add(LSTM(units = 35, return_sequences = True))
regressor.add(Dropout(0.14))

regressor.add(LSTM(units = 35))
regressor.add(Dropout(0.14))

# 输出为股价,故units = 1
regressor.add(Dense(units = 1))

# compile,回归一般loss都是用mean_squared_error,残差平方和
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')

训练LSTM,喂入数据

regressor.fit(X_train, Y_train, epochs = 200, batch_size = 105, callbacks = [checkpoint])

运行结果:请输入图片描述

评估模型在数据集的表现

from keras.models import load_model
regressor = load_model('imdb.hdf5')
test_set = MMsc.transform(test_set)
X_test = []
for i in range(60, len(test_set)):
    X_test.append(test_set[i-60:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
predicted_stock_price = regressor.predict(X_test)
predicted_stock_price = MMsc.inverse_transform(predicted_stock_price)
# 真实股票价格
real_stcok_price = MMsc.inverse_transform(test_set[60:])

结果可视化

import matplotlib.pyplot as plt

plt.figure(figsize=(12, 7))
plt.plot(real_stcok_price, color = 'red', label = '上证指数-真实关盘价')
plt.plot(predicted_stock_price, color = 'blue', label = '上证指数-预测关盘价')
plt.title('上证指数涨跌预测')
plt.ylabel('上证指数关盘价')
plt.legend()
plt.show()

运行结果:请输入图片描述


本文由 白间 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论

0:00