!pip install -Uqq nixtlaTimeGEN-1 快速入门 (Azure)
from nixtla.utils import in_colabIN_COLAB = in_colab()if not IN_COLAB:
from nixtla.utils import colab_badge
from dotenv import load_dotenvTimeGEN-1 是针对 Azure 基础设施优化的 TimeGPT。它是一个适用于时间序列的、经过预训练的生成型变换器,已经准备好投入生产。它能够在只需几行代码的情况下,准确预测零售、能源、金融和物联网等多个领域 🚀。
步骤 1:在 Azure 上设置 TimeGEN-1 端点账户并生成 API 密钥
- 访问 ml.azure.com
- 登录或在 Microsoft 上创建一个账户
- 在侧边栏中点击“模型”
- 在模型目录中搜索“TimeGEN”
- 选择 TimeGEN-1

- 点击“部署”,这将创建一个端点

- 在侧边栏中转到“端点”,您将在那里看到您的TimeGEN-1端点
- 在该端点中有您将使用的基本URL和API密钥

步骤 2:安装 Nixtla
在您最喜欢的Python开发环境中:
使用 pip 安装 nixtla:
pip install nixtla
步骤 3:导入 Nixtla TimeGPT 客户端
from nixtla import NixtlaClient您可以实例化 NixtlaClient 类,提供您的身份验证 API 密钥。
nixtla_client = NixtlaClient(
base_url = "YOUR_BASE_URL",
api_key = "YOUR_API_KEY"
)if not IN_COLAB:
nixtla_client = NixtlaClient()第4步:开始进行预测!
现在你可以开始进行预测了!让我们使用经典的 AirPassengers 数据集导入一个示例。该数据集包含1949年至1960年间澳大利亚航空公司乘客的月度人数。首先,加载数据集并绘制图形:
import pandas as pddf = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv')
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 |
nixtla_client.plot(df, time_col='timestamp', target_col='value')
📘 数据要求
- 确保目标变量列中没有缺失或非数字值。
- 在第一个和最后一个时间戳之间(对于给定的频率)不要包含时间戳中的间隙/跳跃。预测函数不会填补缺失的日期。
- 时间戳列的格式应该是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:要预测的变量。
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...
| 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 |
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...
| 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 |
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...

Give us a ⭐ on Github