TimeGPT 快速开始 (Polars)

!pip install -Uqq nixtla
from nixtla.utils import in_colab
IN_COLAB = in_colab()
if not IN_COLAB:
    from nixtla.utils import colab_badge
    from dotenv import load_dotenv

TimeGPT 是一个生产就绪的、用于时间序列的生成预训练变换器。它能够以几行代码准确预测零售、电力、金融和物联网等多个领域 🚀。

if not IN_COLAB:
    load_dotenv()
    colab_badge('docs/getting-started/21_polars_quickstart')

步骤 1:创建一个 TimeGPT 账户并生成您的 API 密钥

  • 前往 dashboard.nixtla.io
  • 使用 Google、GitHub 或您的电子邮件登录
  • 通过在菜单中转到“API 密钥”并点击“创建新的 API 密钥”来创建您的 API 密钥
  • 您的新密钥将出现。使用右侧的按钮复制 API 密钥。

Dashboard for TimeGPT API keys. Keys is in the middle, with trash and copy buttons on the right.

第2步:安装Nixtla

在您最喜欢的Python开发环境中:

使用 pip 安装 nixtla

pip install nixtla

步骤 3:导入 Nixtla TimeGPT 客户端

from nixtla import NixtlaClient

您可以实例化 NixtlaClient 类,并提供您的身份验证 API 密钥。

nixtla_client = NixtlaClient(
    api_key = 'my_api_key_provided_by_nixtla'
)
if not IN_COLAB:
    nixtla_client = NixtlaClient()

使用 validate_api_key 方法检查您的 API 密钥状态。

nixtla_client.validate_api_key()
True

这将帮助你入门,但有关更安全的使用方式,请参阅 设置你的 API 密钥

第 4 步:开始进行预测!

现在您可以开始进行预测了!让我们使用经典的 AirPassengers 数据集导入一个示例。该数据集包含 1949 年至 1960 年间澳大利亚航空公司乘客的每月数量。首先,加载数据集并绘制图形:

import polars as pl
df = pl.read_csv(
    'https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv',
    try_parse_dates=True,
)
df.head()
shape: (5, 2)
timestamp value
date i64
1949-01-01 112
1949-02-01 118
1949-03-01 132
1949-04-01 129
1949-05-01 121
nixtla_client.plot(df, time_col='timestamp', target_col='value')

📘 数据要求

  • 确保目标变量列没有缺失值或非数值型的值。
  • 在第一时间戳和最后时间戳之间(对于给定的频率)不应包含时间戳的间隔/跳跃。预测功能将不会填补缺失的日期。
  • 时间列应为日期日期时间类型。

有关详细信息,请访问数据要求

预测更长时间的未来

接下来,使用SDK的forecast方法预测接下来的12个月。设置以下参数:

  • df:一个包含时间序列数据的pandas DataFrame。
  • h:预测的步数,即前瞻的时间段。
  • freq:极坐标的偏移别名,可查看可能的值 这里
  • time_col:识别日期戳的列。
  • target_col:要预测的变量。
timegpt_fcst_df = nixtla_client.forecast(df=df, h=12, freq='1mo', time_col='timestamp', target_col='value')
timegpt_fcst_df.head()
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Querying model metadata...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Restricting input...
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
shape: (5, 2)
timestamp TimeGPT
date f64
1961-01-01 437.837921
1961-02-01 426.062714
1961-03-01 463.116547
1961-04-01 478.244507
1961-05-01 505.646484
nixtla_client.plot(df, timegpt_fcst_df, time_col='timestamp', target_col='value')

您还可以通过增加预测范围参数并选择 timegpt-1-long-horizon 模型来生成更长的预测。如果您想预测数据的多个季节周期,请使用此模型。

例如,下面我们预测接下来的36个月:

timegpt_fcst_df = nixtla_client.forecast(df=df, h=36, time_col='timestamp', target_col='value', freq='1mo', model='timegpt-1-long-horizon')
timegpt_fcst_df.head()
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Querying model metadata...
WARNING:nixtla.nixtla_client:The specified horizon "h" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Restricting input...
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
shape: (5, 2)
timestamp TimeGPT
date f64
1961-01-01 436.843414
1961-02-01 419.351532
1961-03-01 458.943146
1961-04-01 477.876068
1961-05-01 505.656921
nixtla_client.plot(df, timegpt_fcst_df, time_col='timestamp', target_col='value')

生成更短的预测

您还可以生成更短的预测。为此,我们建议使用默认模型 timegpt-1

timegpt_fcst_df = nixtla_client.forecast(df=df, h=6, time_col='timestamp', target_col='value', freq='1mo')
nixtla_client.plot(df, timegpt_fcst_df, time_col='timestamp', target_col='value')
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Restricting input...
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...

Give us a ⭐ on Github