工具系列:TimeGPT_(6)同时预测多个时间序列

TimeGPT提供了一个强大的多系列预测解决方案,它涉及同时分析多个数据系列,而不是单个系列。该工具可以使用广泛的系列进行微调,使您能够根据自己的特定需求或任务来定制模型。

python 复制代码
# Import the colab_badge module from the nixtlats.utils package
from nixtlats.utils import colab_badge
python 复制代码
# 导入colab_badge模块,用于在Colab中显示徽章
colab_badge('docs/tutorials/6_multiple_series')
python 复制代码
# 导入load_dotenv函数,用于加载.env文件中的环境变量
from dotenv import load_dotenv
python 复制代码
# 加载环境变量配置文件
load_dotenv()
复制代码
True
python 复制代码
# 导入pandas和TimeGPT模块
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
python 复制代码
# 定义TimeGPT对象,传入token参数,该参数默认值为os.environ.get("TIMEGPT_TOKEN")
# 这里使用了自己提供的token,用于身份验证和访问TimeGPT API

timegpt = TimeGPT(
    token = 'my_token_provided_by_nixtla'
)
python 复制代码
# 创建一个TimeGPT对象,用于生成时间相关的文本。
timegpt = TimeGPT()

以下数据集包含不同电力市场的价格。让我们看看如何进行预测。预测方法的主要参数是包含要预测的时间序列的历史值的输入数据框架。该数据框架可以包含来自许多时间序列的信息。使用"unique_id"列来标识数据集中不同的时间序列。

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

# 显示DataFrame的前几行数据
df.head()

| | unique_id | ds | y |
| 0 | BE | 2016-12-01 00:00:00 | 72.00 |
| 1 | BE | 2016-12-01 01:00:00 | 65.80 |
| 2 | BE | 2016-12-01 02:00:00 | 59.99 |
| 3 | BE | 2016-12-01 03:00:00 | 50.69 |

4 BE 2016-12-01 04:00:00 52.58

让我们使用StatsForecast来绘制这个系列。

python 复制代码
# 调用timegpt模块中的plot函数,并传入df参数
timegpt.plot(df)

我们只需要将数据帧传递给函数,就可以一次性为所有时间序列创建预测。

python 复制代码
# 使用timegpt库中的forecast函数对数据进行预测
# 参数df表示输入的数据框
# 参数h表示预测的时间步长,这里设置为24
# 参数level表示置信水平,这里设置为[80, 90]
timegpt_fcst_multiseries_df = timegpt.forecast(df=df, h=24, level=[80, 90])

# 输出预测结果的前几行
timegpt_fcst_multiseries_df.head()
复制代码
INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
INFO:nixtlats.timegpt:Inferred freq: H
INFO:nixtlats.timegpt:Restricting input...
INFO:nixtlats.timegpt:Calling Forecast Endpoint...

| | unique_id | ds | TimeGPT | TimeGPT-lo-90 | TimeGPT-lo-80 | TimeGPT-hi-80 | TimeGPT-hi-90 |
| 0 | BE | 2016-12-31 00:00:00 | 46.151176 | 36.660475 | 38.337019 | 53.965334 | 55.641878 |
| 1 | BE | 2016-12-31 01:00:00 | 42.426598 | 31.602227 | 33.976717 | 50.876478 | 53.250968 |
| 2 | BE | 2016-12-31 02:00:00 | 40.242889 | 30.439966 | 33.634981 | 46.850798 | 50.045813 |
| 3 | BE | 2016-12-31 03:00:00 | 38.265339 | 26.841481 | 31.022096 | 45.508582 | 49.689197 |

4 BE 2016-12-31 04:00:00 36.618801 18.541384 27.981348 45.256255 54.696218
python 复制代码
# 绘制时间序列图
timegpt.plot(df, timegpt_fcst_multiseries_df, max_insample_length=365, level=[80, 90])
历史预测

您还可以通过添加add_history=True参数来计算历史预测的预测区间。

python 复制代码
# 使用timegpt库中的forecast函数对数据进行预测
# 参数df表示输入的数据框
# 参数h表示预测的时间步长,这里设置为24
# 参数level表示置信水平,这里设置为[80, 90]
# 参数add_history表示是否添加历史数据,这里设置为True
timegpt_fcst_multiseries_with_history_df = timegpt.forecast(df=df, h=24, level=[80, 90], add_history=True)

# 打印预测结果的前几行
timegpt_fcst_multiseries_with_history_df.head()
复制代码
INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
INFO:nixtlats.timegpt:Inferred freq: H
INFO:nixtlats.timegpt:Calling Forecast Endpoint...
INFO:nixtlats.timegpt:Calling Historical Forecast Endpoint...

| | unique_id | ds | TimeGPT | TimeGPT-lo-80 | TimeGPT-lo-90 | TimeGPT-hi-80 | TimeGPT-hi-90 |
| 0 | BE | 2016-12-06 00:00:00 | 55.756325 | 42.066469 | 38.185585 | 69.446180 | 73.327064 |
| 1 | BE | 2016-12-06 01:00:00 | 52.820198 | 39.130342 | 35.249458 | 66.510054 | 70.390938 |
| 2 | BE | 2016-12-06 02:00:00 | 46.851078 | 33.161222 | 29.280338 | 60.540934 | 64.421818 |
| 3 | BE | 2016-12-06 03:00:00 | 50.640884 | 36.951029 | 33.070145 | 64.330740 | 68.211624 |

4 BE 2016-12-06 04:00:00 52.420395 38.730539 34.849655 66.110251 69.991134
python 复制代码
# 绘制时间序列图
timegpt.plot(
    df,  # 数据框,包含要绘制的时间序列数据
    timegpt_fcst_multiseries_with_history_df.groupby('unique_id').tail(365 + 24),  # 根据唯一ID分组的数据框,包含历史数据和预测数据
    max_insample_length=365,  # 最大的历史数据长度
    level=[80, 90],  # 置信水平
)
相关推荐
昵称是6硬币1 小时前
YOLOv11: AN OVERVIEW OF THE KEY ARCHITECTURAL ENHANCEMENTS目标检测论文精读(逐段解析)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉
heimeiyingwang9 天前
【深度学习加速探秘】Winograd 卷积算法:让计算效率 “飞” 起来
人工智能·深度学习·算法
IAM四十二9 天前
Google 端侧 AI 框架 LiteRT 初探
android·深度学习·tensorflow
小白菜3336669 天前
DAY 37 早停策略和模型权重的保存
人工智能·深度学习·算法
yizhimie379 天前
DAY 40 训练和测试的规范写法
深度学习
zeroporn9 天前
以玄幻小说方式打开深度学习词嵌入算法!! 使用Skip-gram来完成 Word2Vec 词嵌入(Embedding)
人工智能·深度学习·算法·自然语言处理·embedding·word2vec·skip-gram
丶Darling.9 天前
深度学习与神经网络 | 邱锡鹏 | 第五章学习笔记 卷积神经网络
深度学习·神经网络·学习
大模型最新论文速读9 天前
Agent成本降低46%:缓存规划器的思路模板
人工智能·深度学习·机器学习·缓存·语言模型·自然语言处理
丶Darling.9 天前
深度学习与神经网络 | 邱锡鹏 | 第七章学习笔记 网络优化与正则化
深度学习·神经网络·学习
SHIPKING3939 天前
【机器学习&深度学习】机器学习核心的计算公式:wx+b
人工智能·深度学习·机器学习