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

  • 访问 dashboard.nixtla.io
  • 使用 Google、GitHub 或您的电子邮件登录
  • 在菜单中进入“API Keys”,然后点击“Create New API Key”来创建您的 API 密钥
  • 您的新密钥将会出现。使用右侧的按钮复制该 API 密钥。

步骤 2:安装 Nixtla

在您喜欢的 Python 开发环境中

使用 pip 安装 nixtla

pip install nixtla

步骤 3:导入 Nixtla TimeGPT 客户端

from nixtla import NixtlaClient

您可以通过提供您的身份验证 API 密钥来实例化 NixtlaClient 类。

nixtla_client = NixtlaClient(
    api_key = 'my_api_key_provided_by_nixtla'
)

使用 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()
timestampvalue
datei64
1949-01-01112
1949-02-01118
1949-03-01132
1949-04-01129
1949-05-01121
nixtla_client.plot(df, time_col='timestamp', target_col='value')

📘 数据要求

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

更多详细信息请参阅 数据要求

预测更长远的未来

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

  • df:包含时间序列数据的 pandas DataFrame。
  • h:Horizons 是预测的未来步数。
  • freq:polars 的偏移别名,请参阅此处可能的取值。
  • 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...
timestampTimeGPT
datef64
1961-01-01437.837921
1961-02-01426.062714
1961-03-01463.116547
1961-04-01478.244507
1961-05-01505.646484
nixtla_client.plot(df, timegpt_fcst_df, time_col='timestamp', target_col='value')

您还可以通过增加 horizon 参数并选择 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...
timestampTimeGPT
datef64
1961-01-01436.843414
1961-02-01419.351532
1961-03-01458.943146
1961-04-01477.876068
1961-05-01505.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...