# 准备输入序列 last_sequence = df[valid_independent_fields].values[-self.lstm_prediction_time_steps:] last_sequence = last_sequence.reshape((1, self.lstm_prediction_time_steps, len(valid_independent_fields))) future_predictions = [] forecast_timestamps = pd.date_range(df[self.timestamp_field].iloc[-1], periods=forecast_steps + 1, freq=f'{avg_interval_minutes:.0f}T')[1:] for _ in range(forecast_steps): next_pred = model.predict(last_sequence) future_predictions.append(next_pred[0, 0]) # 更新输入序列,使用预测值来代替最早的一个时间点 next_input = np.roll(last_sequence, -1, axis=1) next_input[0, -1, 0] = next_pred last_sequence = next_input 最难理解的是后面3句。把原来自变量的df也就是last_sequence往前滚动一行,再把第一行放在最后一行,再把预测的因变量放在最后一行的第一个,这个逻辑完全就是瞎胡闹。看看chatgpt怎么解释的。 问: 我还是没弄明白。我们来看: last_sequence = df[valid_independent_fields].values[-self.lstm_prediction_time_steps:] last_sequence = last_sequence.reshape((1, self.lstm_prediction_time_steps, len(valid_independent_fields))) 也就是说,last_sequence 是从多个自变量组成的二维DF里取出来的。再看下一句: next_pred = model.predict(last_sequence) 也就是说未来的因变量是由last_sequence里的自变量数组预测出来的。所以,预测的逻辑是通过过去一段时间的自变量去预测未来的因变量。 现在,假如我们已经预测出一个新的未来的因变量,这个因变量把它放进last_sequence 这个自变量数据组里,这是什么逻辑?我不明白?
chatgpt:
你提的问题是非常关键的,也是理解时间序列预测中递归预测过程的核心。让我们一步步来解开这个逻辑。
### 回顾预测过程的逻辑
1. **训练阶段**:在训练阶段,你的模型是通过输入的过去一段时间的自变量序列来预测对应的因变量。例如,你有一个长度为 `n` 的时间窗口,每个时间步有多个自变量,模型使用这些自变量序列来学习如何预测因变量。
2. **预测阶段**:在预测阶段,模型输入的是过去时间窗口内的自变量序列,然后输出的是预测的因变量。
### 递归预测的逻辑
在递归预测过程中,有一个假设:未来的因变量值可以通过历史的自变量序列预测出来,但在预测过程中,未来的自变量可能不再完全依赖于真实的历史数据,而可能需要使用模型预测的因变量来更新输入序列。这也是递归预测的核心,即使用模型的输出(预测的因变量)作为后续预测