You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用已搭建LSTM模型实现股票价格多步预测 覆盖未来一年预测需求

实现未来360天股价预测的调整方案

核心实现逻辑

你当前训练的LSTM模型逻辑是输入过去60天的收盘价,输出第61天的收盘价,要实现长期预测需要用滚动迭代的方式:每生成1天的预测值,就把该值追加到输入序列末尾,同时舍弃序列最早的1天数据,保持输入长度始终为60天,循环360次即可得到下一年的预测结果。

补充代码实现

你可以在现有代码的#for future 360 days what can I do?....位置追加以下代码:

import matplotlib.pyplot as plt

# 1. 准备初始预测输入:取全量数据的最后60天归一化数据
last_60_days = scaled_data[-n:]
current_input = last_60_days.reshape(1, n, 1)

future_predictions_scaled = []
# 2. 滚动预测360天
for _ in range(360):
    # 预测下1天
    next_day_scaled = model.predict(current_input, verbose=0)
    future_predictions_scaled.append(next_day_scaled[0][0])
    # 更新输入序列:去掉最早的1天,加入新预测的1天
    current_input = np.append(current_input[:, 1:, :], next_day_scaled.reshape(1, 1, 1), axis=1)

# 3. 反归一化得到真实价格
future_predictions = scaler.inverse_transform(np.array(future_predictions_scaled).reshape(-1, 1))

# 4. 生成未来的日期序列:freq='B'代表仅交易日,需要自然日可以改成'D'
last_date = df.index[-1]
future_dates = pd.date_range(start=last_date + pd.Timedelta(days=1), periods=360, freq='B')

绘制走势图表

把历史数据和未来预测数据合并绘制:

plt.figure(figsize=(16,8))
# 绘制历史收盘价
plt.plot(df.index, dataset, label='历史收盘价')
# 绘制未来预测收盘价
plt.plot(future_dates, future_predictions, label='未来1年预测收盘价', color='red')
plt.title('股价历史走势+未来1年预测')
plt.xlabel('日期')
plt.ylabel('收盘价')
plt.legend()
plt.show()

效果优化建议

  • 你当前模型只训练了1轮(epochs=1),拟合效果会很差,建议调整为20~50轮,同时可以加入早停机制防止过拟合
  • 股价本身随机性极强,LSTM单特征预测的长期结果误差会越来越大,建议加入成交量、大盘指数等更多特征提升准确率
  • 滚动预测时可以加入蒙特卡洛模拟生成置信区间,避免只输出单条曲线的误导性

内容的提问来源于stack exchange,提问作者Hossein Ghaffari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 08:15:07