当前位置: 首页 > news >正文

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

文章目录

    • TimeGPT使用外生变量时间序列预测
    • 导入相关工具包
    • 预测欧美国家次日电力价格案例

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.htmlfrom 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'>

http://www.lryc.cn/news/267981.html

相关文章:

  • 应用于前端的自然断点法:wasm应用示例
  • web前端javaScript笔记——(6)数组
  • vue的props
  • 【51单片机系列】DS18B20温度传感器模块
  • ubuntu20.04 conda环境配置Mask2former记录
  • Git更新远程分支
  • 2023年软件工程师工作总结范文
  • STM32实战之深入理解I²C通信协议
  • 如何区分ChatGPT 3.5与ChatGPT 4:洞悉智能对话的新时代
  • blender scripting 编写
  • 使用 Elasticsearch 检测抄袭 (二)
  • WPF DataGrid
  • 【cesium-5】鼠标交互与数据查询
  • Xcode 编译速度慢是什么原因?如何提高编译速度?
  • Best Arm Identification in Batched Multi-armed Bandit Problems
  • Unity编辑器紫色
  • 冒泡排序(C语言)
  • Unity中Shader观察空间推导(在Shader中实现)
  • Hive04_DDL操作
  • odoo17核心概念view4——view.js
  • Centos7 openSSL
  • Web 安全之文件下载漏洞详解
  • 搬运机器人RFID传感器CNS-RFID-01|1S的RS485(MODBUS|HS协议)通讯连接方法
  • 使用ZMQ.proxy实现ZMQ PUB消息转发
  • 若依SQL Server开发使用教程
  • Mysql5.7服务器选项、系统变量和状态变量参考
  • 【Qt-Qss-Style】
  • 基于yolov8,制作停车位计数器(附源码)
  • C++设计模式:单例模式(饿汉式、懒汉式)
  • Django 访问前端页面一直在转异常:ReferenceError:axios is not defined