工具系列:TimeGPT_(2)使用外生变量时间序列预测

发布时间:2023年12月26日

TimeGPT使用外生变量时间序列预测

外生变量在时间序列预测中非常重要,因为它们提供了可能影响预测的额外信息。这些变量可以包括假日标记、营销支出、天气数据或与你正在预测的时间序列数据相关的任何其他外部数据。

例如,如果你正在预测冰淇淋销售额,温度数据可以作为一个有用的外生变量。在炎热的天气里,冰淇淋销售额可能会增加。

要在TimeGPT中加入外生变量,你需要将时间序列数据中的每个点与相应的外部数据配对。

导入相关工具包


# Importing the colab_badge module from the nixtlats.utils package
from nixtlats.utils import colab_badge

# 导入load_dotenv函数,用于加载.env文件中的环境变量
from dotenv import load_dotenv
# 导入load_dotenv函数,用于加载环境变量
load_dotenv()
True

import pandas as pd
from nixtlats import TimeGPT

/home/ubuntu/miniconda/envs/nixtlats/lib/python3.11/site-packages/statsforecast/core.py:25: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from tqdm.autonotebook import tqdm


# 定义TimeGPT对象,并传入一个参数token,用于身份验证
# 如果没有提供token参数,则默认使用os.environ.get("TIMEGPT_TOKEN")获取token
timegpt = TimeGPT(
    token = 'my_token_provided_by_nixtla'
)
# 导入TimeGPT模型

timegpt = TimeGPT()  # 创建TimeGPT对象的实例

预测欧美国家次日电力价格案例

让我们看一个关于预测次日电力价格的例子。以下数据集包含了欧洲和美国五个市场的每小时电力价格(y列),这些市场由unique_id列进行标识。从Exogenous1day_6的列是TimeGPT用来预测价格的外生变量。

# 从指定的URL读取csv文件,并将其存储在DataFrame对象df中
df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short-with-ex-vars.csv')

# 显示DataFrame对象df的前几行数据
df.head()
unique_iddsyExogenous1Exogenous2day_0day_1day_2day_3day_4day_5day_6
0BE2016-12-01 00:00:0072.0061507.071066.00.00.00.01.00.00.00.0
1BE2016-12-01 01:00:0065.8059528.067311.00.00.00.01.00.00.00.0
2BE2016-12-01 02:00:0059.9958812.067470.00.00.00.01.00.00.00.0
3BE2016-12-01 03:00:0050.6957676.064529.00.00.00.01.00.00.00.0
4BE2016-12-01 04:00:0052.5856804.062773.00.00.00.01.00.00.00.0

为了生成预测,我们还需要添加外生变量的未来值。让我们读取这个数据集。在这种情况下,我们希望预测未来24个步骤,因此每个“unique_id”将有24个观察值。

# 从GitHub上读取电力短期未来外部变量数据集
future_ex_vars_df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short-future-ex-vars.csv')

# 打印数据集的前五行
future_ex_vars_df.head()
unique_iddsExogenous1Exogenous2day_0day_1day_2day_3day_4day_5day_6
0BE2016-12-31 00:00:0064108.070318.00.00.00.00.00.01.00.0
1BE2016-12-31 01:00:0062492.067898.00.00.00.00.00.01.00.0
2BE2016-12-31 02:00:0061571.068379.00.00.00.00.00.01.00.0
3BE2016-12-31 03:00:0060381.064972.00.00.00.00.00.01.00.0
4BE2016-12-31 04:00:0060298.062900.00.00.00.00.00.01.00.0

让我们调用forecast方法,添加这些信息:

# 使用timegpt模型对数据进行预测
# 参数说明:
# - df: 历史数据的DataFrame
# - X_df: 未来外部变量的DataFrame
# - h: 预测的时间步长
# - level: 置信水平
timegpt_fcst_ex_vars_df = timegpt.forecast(df=df, X_df=future_ex_vars_df, h=24, level=[80, 90])

# 打印预测结果的前几行
timegpt_fcst_ex_vars_df.head()
INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
INFO:nixtlats.timegpt:Inferred freq: H
INFO:nixtlats.timegpt:Calling Forecast Endpoint...
unique_iddsTimeGPTTimeGPT-lo-90TimeGPT-lo-80TimeGPT-hi-80TimeGPT-hi-90
0BE2016-12-31 00:00:0038.86176233.82107334.36866943.35485443.902450
1BE2016-12-31 01:00:0035.38210230.01459431.49332239.27088240.749610
2BE2016-12-31 02:00:0033.81142526.65882128.54308739.07976440.964029
3BE2016-12-31 03:00:0031.70747524.89620526.81879536.59615538.518745
4BE2016-12-31 04:00:0030.31647521.12514324.43214836.20080139.507807
# 导入必要的模块和函数

# 使用timegpt.plot函数绘制时间序列预测结果的图表
# 参数1:df[['unique_id', 'ds', 'y']],表示要绘制的时间序列数据,包括唯一标识符、时间戳和目标变量
# 参数2:timegpt_fcst_ex_vars_df,表示时间序列预测的额外变量数据
# 参数3:max_insample_length=365,表示用于训练模型的最大历史数据长度为365天
# 参数4:level=[80, 90],表示绘制置信区间的水平,这里设置为80%和90%
# 返回:绘制好的时间序列预测结果图表
timegpt.plot(
    df[['unique_id', 'ds', 'y']], 
    timegpt_fcst_ex_vars_df, 
    max_insample_length=365, 
    level=[80, 90], 
)

我们还可以获得特征的重要性。

# 绘制水平条形图
timegpt.weights_x.plot.barh(x='features', y='weights')

<Axes: ylabel='features'>

您还可以使用CountryHolidays类添加国家假期。

# 导入nixtlats.date_features模块中的CountryHolidays类

from nixtlats.date_features import CountryHolidays
# 导入所需的模块和函数

# 使用timegpt模型对给定的数据进行预测
# 参数:
# - df: 历史数据的DataFrame,包含时间序列数据
# - X_df: 未来外部变量的DataFrame,包含与时间序列相关的外部变量
# - h: 预测的时间步长,即预测未来多少个时间点的值
# - level: 置信水平的列表,用于计算置信区间
# - date_features: 日期特征的列表,用于考虑特殊的日期效应,如假期等
# 返回值:
# - timegpt_fcst_ex_vars_df: 预测结果的DataFrame,包含预测值和置信区间
timegpt_fcst_ex_vars_df = timegpt.forecast(
    df=df, X_df=future_ex_vars_df, h=24, level=[80, 90], 
    date_features=[CountryHolidays(['US'])]
)
# 使用timegpt模型的weights_x属性绘制水平条形图
# 参数:
# - x: 水平条形图的x轴数据,即特征名称
# - y: 水平条形图的y轴数据,即特征权重值
timegpt.weights_x.plot.barh(x='features', y='weights')
INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
INFO:nixtlats.timegpt:Inferred freq: H
INFO:nixtlats.timegpt:Calling Forecast Endpoint...





<Axes: ylabel='features'>

文章来源:https://blog.csdn.net/wjjc1017/article/details/135233108
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。