本文共 2234 字,大约阅读时间需要 7 分钟。
在金融领域,时间序列预测一直是研究热点之一,而LSTM(长短期记忆网络)作为一种强大的序列建模方法,近年来在股票价格预测中取得了显著成果。本文将介绍一个简单的LSTM模型,用于基于历史数据进行股票价格预测。
首先,我们需要加载时间序列数据集。以下是加载数据的代码示例:
import pandas as pddata = pd.read_csv('SP500.csv')data.head()
数据集包含股票价格的历史数据,选择Close
列作为目标特征进行预测。为了便于模型训练,我们对数据进行归一化处理。
import numpy as npfrom sklearn.preprocessing import MinMaxScalerdata_close = np.reshape(data['Close'].values, (-1, 1))scaler = MinMaxScaler(feature_range=(0, 1))scaled_data = scaler.fit_transform(data_close)
归一化后的数据范围被限制在[0, 1]之间,这有助于加速模型收敛。
LSTM的输入格式要求为 [batch_size, num_steps, feature_size]
,其中num_steps
表示每次预测使用的历史观测数,feature_size
为每个观测的特征维度。在本例中,feature_size
设为1,因为我们只预测一个未知数。
def data_format(data, num_steps=4, test_ratio=0.2): # 将数据分割为多个时间窗口 X = np.array([data[i:i + num_steps] for i in range(len(data) - num_steps)]) y = np.array([data[i + num_steps] for i in range(len(data) - num_steps)]) # 划分训练集和测试集 train_size = int(len(X) * (1 - test_ratio)) train_X, test_X = X[:train_size], X[train_size:] train_y, test_y = y[:train_size], y[train_size:] return train_X, train_y, test_X, test_y# 生成训练和测试数据集num_steps = 3test_ratio = 0.2train_X, train_y, test_X, test_y = data_format(scaled_data, num_steps, test_ratio)print(train_X.shape, test_X.shape) # 输出 tensor shape
接下来,我们定义LSTM网络结构。输入数据为 [batch_size, num_steps, 1]
,LSTM层输出为32维向量,后接一个全连接层将其映射为预测结果。
from keras.layers import LSTM, Dense, Inputfrom keras import Modelinput_tensor = Input(shape=[num_steps, 1])lstm_layer = LSTM(32)(input_tensor)output_layer = Dense(1)(lstm_layer)model = Model(inputs=input_tensor, outputs=output_layer)model.compile(loss='mean_squared_error', optimizer='adam')model.summary()
模型总参数数量为4385,训练参数数量同样为4385。
训练模型使用以下代码:
model.fit(train_X, train_y, batch_size=1, epochs=30)
训练完成后,我们对测试集进行预测。
preds = model.predict(test_X)# 反归一化预测结果scaled_pred = scaler.inverse_transform(preds)# 反归一化真实值true_values = scaler.inverse_transform(test_y)plt.plot(preds, c='r', label='预测值')plt.plot(true_values, c='b', label='真实值')plt.legend()plt.show()
从预测结果可以看出,模型对测试集的预测具有较高的拟合度。虽然模型表现优异,但可以进一步优化模型结构,例如增加LSTM层数以提升预测精度。
本文介绍了一个简单的LSTM模型用于股票价格预测。通过对数据进行归一化和格式规范化,成功训练出一个基本性能的预测模型。对于更复杂的场景,可以考虑增加LSTM层数,或引入注意力机制,以提升模型性能。
转载地址:http://znkg.baihongyu.com/