步骤 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()
INFO:nixtla.nixtla_client:Happy Forecasting! :), If you have questions or need support, please email support@nixtla.io
True

这将帮助您入门,但为了更安全地使用,请参阅 设置您的 API 密钥

步骤 4:开始进行预测!

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

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv')
df.head()
时间戳
01949-01-01112
11949-02-01118
21949-03-01132
31949-04-01129
41949-05-01121
nixtla_client.plot(df, time_col='timestamp', target_col='value')

📘 数据要求

  • 确保目标变量列没有缺失值或非数值。
  • 在第一个和最后一个日期戳之间,不要包含(给定频率下的)日期戳的间隔/跳跃。预测函数不会填充缺失日期。
  • 日期戳列的格式应能被 Pandas 读取(更多详情请参见 此链接)。

如需更多详情,请访问 数据要求

👍 保存使用 TimeGPT 制作的图表

在 Notebook 环境中,plot 方法会自动显示图表。要在本地保存图表,您可以这样做

fig = nixtla_client.plot(df, time_col='timestamp', target_col='value')

fig.savefig('plot.png', bbox_inches='tight')

预测更远的未来

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

  • df:包含时间序列数据的 Pandas DataFrame。
  • h:预测步长,即向前预测的步数。
  • freq:时间序列的频率,使用 Pandas 格式。请参阅 Pandas 可用的频率别名。(如果您未提供任何频率,SDK 将尝试推断)
  • time_col:标识日期戳的列。
  • target_col:要预测的变量。
timegpt_fcst_df = nixtla_client.forecast(df=df, h=12, freq='MS', time_col='timestamp', target_col='value')
timegpt_fcst_df.head()
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...
时间戳TimeGPT
01961-01-01437.837921
11961-02-01426.062714
21961-03-01463.116547
31961-04-01478.244507
41961-05-01505.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='MS', model='timegpt-1-long-horizon')
timegpt_fcst_df.head()
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
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:Restricting input...
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
时间戳TimeGPT
01961-01-01436.843414
11961-02-01419.351532
21961-03-01458.943146
31961-04-01477.876068
41961-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='MS')
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...