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

为何基于Keras的双向LSTM彩票预测模型仅输出单个测试预测结果?

问题描述

参考文章构建了用于彩票号码预测的双向LSTM模型,模型结构如下:

model=keras.Sequential()
model.add(Bidirectional(LSTM(240, input_shape = (tamaño_ventana, num_features), return_sequences = True)))
model.add(Dropout(0.2))
model.add(Bidirectional(LSTM(240, input_shape=(tamaño_ventana, num_features), return_sequences=True)))
model.add(Dropout(0.2))
model.add(Bidirectional(LSTM(240, input_shape=(tamaño_ventana, num_features), return_sequences=True)))
model.add(Bidirectional(LSTM(240, input_shape=(tamaño_ventana, num_features), return_sequences=False)))
model.add(Dense(59))
model.add(Dense(num_features))

变量定义:

num_filas=df_train.values.shape[0]
tamaño_ventana=7#每次预测参考的往期开奖次数
num_features=df_train.values.shape[1]#每期开奖的号码数量

采用80%数据训练、20%数据测试的划分方式,但调用model.predict()时仅返回单个预测结果,无法对全部测试数据进行对比。预测代码如下:

y_pred = loaded_model.predict(np.array([df_transformada_test]))
print("The predicted numbers in the last lottery game are:", scaler_test.inverse_transform(y_pred).astype(int)[0])
prediction = np.array(df_test.tail(1))
print("The actual numbers in the last lottery game were:", prediction[0])

(df_transformada_test是调整为正确形状的20%测试数据)

问题原因
  • 输入多套了一层维度:预测时用np.array([df_transformada_test])给测试数据额外加了一个维度。假设df_transformada_test的正确形状是(样本数, tamaño_ventana, num_features),套上np.array([])后会变成(1, 样本数, tamaño_ventana, num_features),模型会把这个多余的第一维度当成批量大小,最终只输出1个预测结果。直接传入df_transformada_test即可,不需要额外包裹。
  • 测试集窗口构造不完整:如果df_transformada_test本身只生成了对应最后一期预测的单个窗口数据,那模型自然只会返回一个结果。需要确认测试集的窗口滑动逻辑是否覆盖了所有需要预测的测试样本——比如测试集有N个开奖结果,就应该生成N个对应的输入窗口(每个窗口包含前7期数据)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 19:25:29