NeuralForecast 外生变量
flyfish
推理
import pandas as pd
import numpy as np
from neuralforecast.core import NeuralForecast# 新的输入数据
new_data = pd.DataFrame({"unique_id": [1.0, 1.0],"ds": pd.to_datetime(["1949-01-31", "1949-02-28"]),"y": [112.0, 118.0],}
)# 确保数据的顺序和索引是正确的
new_data = new_data.reset_index(drop=True)
print("New input data:")
print(new_data)# 加载已保存的模型
nf2 = NeuralForecast.load(path="./checkpoints/test_run/")# 使用已加载的模型进行预测
Y_hat_df = nf2.predict(df=new_data).reset_index()
print("Prediction results:")
print(Y_hat_df)
前向传播
LSTM SAMPLING_TYPE = "recurrent"windows_batch: {'insample_y': tensor([[[-1.0000],[ 1.0000]]]), 'insample_mask': tensor([[[1.], [1.]]]), 'futr_exog': None, 'hist_exog': None, 'stat_exog': None
}nhits SAMPLING_TYPE = "windows"windows_batch: {'insample_y': tensor([[ 0., 0., 112., 118.]]), 'insample_mask': tensor([[0., 0., 1., 1.]]), 'futr_exog': None, 'hist_exog': None, 'stat_exog': None
}
输入数据通常不仅仅是时间序列本身,还包括各种外生变量。这些外生变量可以帮助模型更好地理解时间序列的变化模式。下面逐个解释这些变量在模型中的作用。
代码
encoder_input = windows_batch["insample_y"] # [B, seq_len, 1]
futr_exog = windows_batch["futr_exog"]
hist_exog = windows_batch["hist_exog"]
stat_exog = windows_batch["stat_exog"]
变量解释
encoder_input (windows_batch[“insample_y”])
用途:这是模型的主要输入数据,表示历史时间序列的实际值。
形状:[B, seq_len, 1]
B:批次大小。
seq_len:时间序列的长度。
1:单个特征维度(时间序列的值)。
futr_exog (windows_batch[“futr_exog”])
用途:表示未来的外生变量,这些变量在预测时间段内是已知的。可以是天气预报、未来的计划活动、节假日等。
形状:[B, futr_len, num_futr_features]
B:批次大小。
futr_len:未来时间步的数量。
num_futr_features:未来外生变量的特征数量。
hist_exog (windows_batch[“hist_exog”])
用途:表示历史的外生变量,这些变量在过去的时间段内是已知的。可以是过去的天气、过去的活动记录等。
形状:[B, hist_len, num_hist_features]
B:批次大小。
hist_len:历史时间步的数量。
num_hist_features:历史外生变量的特征数量。
stat_exog (windows_batch[“stat_exog”])
用途:表示静态外生变量,这些变量在整个时间段内保持不变。可以是地区、用户特征、商品类别等。
形状:[B, num_stat_features]
B:批次大小。
num_stat_features:静态外生变量的特征数量。
简述
变量的作用
encoder_input
主要作用:作为模型的主要输入,提供实际的历史时间序列数据。模型使用这些数据来学习时间序列的模式。
futr_exog
主要作用:为模型提供未来已知的外生变量。这些变量可以帮助模型在预测未来时利用更多的信息,从而提高预测精度。
hist_exog
主要作用:为模型提供历史已知的外生变量。这些变量可以帮助模型理解历史时间序列中的影响因素,从而更好地进行建模。
stat_exog
主要作用:为模型提供静态的外生变量。这些变量在整个时间段内保持不变,能够帮助模型了解数据的整体特征或背景信息。