!pip install -Uqq nixtla
TimeGPT 快速入门
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 是一个生产就绪的生成预训练变换器,用于时间序列。它能够仅用几行代码准确预测零售、电力、金融和物联网等多个领域 🚀。
步骤 1:创建 TimeGPT 账户并生成您的 API 密钥
- 前往 dashboard.nixtla.io/freetrial 激活您的免费试用并设置账户。
- 使用 Google、GitHub 或您的电子邮件登录
- 通过在菜单中前往 ‘API 密钥’ 并点击 ‘创建新的 API 密钥’ 来创建您的 API 密钥
- 您的新密钥将会出现。使用右侧的按钮复制 API 密钥。
步骤 2:安装 Nixtla
在您最喜欢的Python开发环境中:
使用 pip
安装 nixtla
:
pip install nixtla
步骤 3:导入 Nixtla TimeGPT 客户端
from nixtla import NixtlaClient
您可以实例化 NixtlaClient
类,并提供您的身份验证 API 密钥。
= NixtlaClient(
nixtla_client = 'my_api_key_provided_by_nixtla'
api_key )
if not IN_COLAB:
= NixtlaClient() nixtla_client
使用validate_api_key
方法检查您的API密钥状态。
nixtla_client.validate_api_key()
INFO:nixtla.nixtla_client:Happy Forecasting! :), If you have questions or need support, please email ops@nixtla.io
True
这将帮助你入门,但有关更安全的使用方法,请参见 设置您的 API 密钥。
第4步:开始进行预测!
现在你可以开始进行预测了!让我们使用经典的 AirPassengers
数据集导入一个示例。这个数据集包含了1949年到1960年间澳大利亚每月的航空乘客数量。首先,加载数据集并绘制图表:
import pandas as pd
= pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv')
df df.head()
timestamp | value | |
---|---|---|
0 | 1949-01-01 | 112 |
1 | 1949-02-01 | 118 |
2 | 1949-03-01 | 132 |
3 | 1949-04-01 | 129 |
4 | 1949-05-01 | 121 |
='timestamp', target_col='value') nixtla_client.plot(df, time_col
📘 数据要求
- 确保目标变量列没有缺失值或非数值类型的值。
- 在第一个和最后一个日期戳之间(对于给定的频率),不要包含日期戳中的空缺/跳跃。预测功能不会填补缺失的日期。
- 日期戳列的格式应该能够被Pandas读取(有关更多详细信息,请参见此链接)。
有关更多详细信息,请访问数据要求。
👍 使用 TimeGPT 保存图形
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
:要预测的变量。
= nixtla_client.forecast(df=df, h=12, freq='MS', time_col='timestamp', target_col='value')
timegpt_fcst_df 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...
timestamp | TimeGPT | |
---|---|---|
0 | 1961-01-01 | 437.837921 |
1 | 1961-02-01 | 426.062714 |
2 | 1961-03-01 | 463.116547 |
3 | 1961-04-01 | 478.244507 |
4 | 1961-05-01 | 505.646484 |
='timestamp', target_col='value') nixtla_client.plot(df, timegpt_fcst_df, time_col
您还可以通过增加时间范围参数并选择 timegpt-1-long-horizon
模型来生成更长的预测。如果您想预测数据的多个季节周期,请使用此模型。
例如,让我们预测接下来的36个月:
= nixtla_client.forecast(df=df, h=36, time_col='timestamp', target_col='value', freq='MS', model='timegpt-1-long-horizon')
timegpt_fcst_df 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...
timestamp | TimeGPT | |
---|---|---|
0 | 1961-01-01 | 436.843414 |
1 | 1961-02-01 | 419.351532 |
2 | 1961-03-01 | 458.943146 |
3 | 1961-04-01 | 477.876068 |
4 | 1961-05-01 | 505.656921 |
='timestamp', target_col='value') nixtla_client.plot(df, timegpt_fcst_df, time_col
生成更短的预测
您还可以生成更短的预测。为此,我们建议使用默认模型 timegpt-1
。
= nixtla_client.forecast(df=df, h=6, time_col='timestamp', target_col='value', freq='MS')
timegpt_fcst_df ='timestamp', target_col='value') nixtla_client.plot(df, timegpt_fcst_df, time_col
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