Tool series: TimeGPT_(1) How to obtain token and initial use

introduce

Nixtla's TimeGPT is a generative pre-trained predictive model for time series data. TimeGPT can generate accurate forecasts for new time series without training, using only historical values ​​as input. TimeGPT can be used for a variety of tasks, including demand forecasting, anomaly detection, financial forecasting, and more.

TimeGPT leverages the Transformer architecture and a self-attention mechanism based on pioneering work from Google and the University of Toronto in 2017. The model was trained on 100 billion data points from publicly available data.

The TimeGPT model works similar to how humans read sentences - from left to right. It looks at windows of past data, which we can think of as "marks", and predicts what will happen next. This prediction is based on patterns identified by the model in past data and extrapolated into the future.

The API provides an interface with TimeGPT, allowing users to leverage its predictive capabilities to predict future events. TimeGPT can also be used for other time series-related tasks, such as what-if scenarios, anomaly detection, etc.

Insert image description here

Get Token

1. Official website:
https://docs.nixtla.io

2. Log in entrance:
Insert image description here
Log in in the red box
3. Log in to the console
Insert image description here

4. Create Token
Insert image description here

5. Check the number of uses.
Please add image description
You can use it 420 times for free.

usage


# Import the colab_badge module from the nixtlats.utils package
from nixtlats.utils import colab_badge
# 导入colab_badge函数
from IPython.display import colab_badge

# 调用colab_badge函数,显示Colab徽章
colab_badge('docs/getting-started/1_getting_started_short')
# 导入load_dotenv函数,用于加载.env文件中的环境变量
from dotenv import load_dotenv
# 加载dotenv模块,用于从.env文件中加载环境变量
load_dotenv()
True
# 导入TimeGPT模块
from nixtlats import TimeGPT
/home/ubuntu/miniconda/envs/nixtlats/lib/python3.11/site-packages/statsforecast/core.py:25: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from tqdm.autonotebook import tqdm

You instantiate TimeGPTthe class and provide your credentials.

# 导入TimeGPT类
from timegpt import TimeGPT

# 创建TimeGPT对象,传入token参数,如果没有提供则默认使用环境变量中的TIMEGPT_TOKEN
timegpt = TimeGPT(token='my_token_provided_by_nixtla')
# 创建一个TimeGPT对象,用于生成时间相关的文本。
timegpt = TimeGPT()

You can validate_tokentest the validity of your token by calling a method.

# 调用timegpt模块的validate_token()函数进行token验证
timegpt.validate_token()
INFO:nixtlats.timegpt:Happy Forecasting! :), If you have questions or need support, please email [email protected]





True

Now you can start making predictions! Let's import a classic AirPassengersdataset example. This data set contains the monthly number of airline passengers in Australia between 1949 and 1960. First, let's load the dataset and plot it:

# 导入pandas库,用于数据处理和分析
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv')

# 显示数据集的前5行
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
# 使用timegpt模块的plot函数绘制图表
# 参数df为数据集,time_col为时间列,target_col为目标列
timegpt.plot(df, time_col='timestamp', target_col='value')

Important requirements for data

  • Make sure the target variable column has no missing or non-numeric values.
  • Do not include a gap/jump in date stamps between the first and last date stamp (for a given frequency). The forecast function does not fill in missing dates.
  • The date stamp column should be in a format that can be read by Pandas (see this ).

Next, use the SDK's forecastmethod to predict the next 12 months. Set the following parameters:

  • df: pandas dataframe containing time series data.
  • h: The number of steps to predict.
  • freq: Frequency of the time series, using Pandas format. See available frequencies in pandas .
  • time_col: Column that identifies the date timestamp column.
  • target_col: the variable we want to predict.
# 使用timegpt库中的forecast函数对数据进行预测
# 参数说明:
# - df: 待预测的数据框
# - h: 预测的时间步数
# - freq: 预测的时间频率
# - time_col: 时间列的列名
# - target_col: 目标列的列名
timegpt_fcst_df = timegpt.forecast(df=df, h=12, freq='MS', time_col='timestamp', target_col='value')

# 打印预测结果的前几行
timegpt_fcst_df.head()
INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
INFO:nixtlats.timegpt:Calling Forecast Endpoint...
timestamp TimeGPT
0 1961-01-01 437.837952
1 1961-02-01 426.062744
2 1961-03-01 463.116577
3 1961-04-01 478.244507
4 1961-05-01 505.646484

# 导入必要的库
import timegpt

# 使用timegpt库中的plot函数绘制图表
# 参数df为原始数据集,timegpt_fcst_df为预测数据集
# time_col参数指定时间列的名称为'timestamp'
# target_col参数指定目标列的名称为'value'
timegpt.plot(df, timegpt_fcst_df, time_col='timestamp', target_col='value')

You can also generate longer forecasts by increasing the time range parameter. For example, let's forecast the next 36 months:

# 使用timegpt模块中的forecast函数对数据进行预测
# 参数df表示输入的数据框
# 参数h表示预测的时间步数
# 参数time_col表示时间列的名称
# 参数target_col表示目标列的名称
# 参数freq表示数据的频率
timegpt_fcst_df = timegpt.forecast(df=df, h=36, time_col='timestamp', target_col='value', freq='MS')

# 打印预测结果的前几行
timegpt_fcst_df.head()
INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
WARNING:nixtlats.timegpt:The specified horizon "h" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.
INFO:nixtlats.timegpt:Calling Forecast Endpoint...
timestamp TimeGPT
0 1961-01-01 437.837952
1 1961-02-01 426.062744
2 1961-03-01 463.116577
3 1961-04-01 478.244507
4 1961-05-01 505.646484
# 使用timegpt模块中的plot函数来绘制图表
# 参数df是原始数据,timegpt_fcst_df是预测数据
# time_col指定时间列的名称,target_col指定目标列的名称
timegpt.plot(df, timegpt_fcst_df, time_col='timestamp', target_col='value')

Or a shorter one:

# 使用timegpt模块的forecast函数对数据进行预测
# 参数df表示输入的数据框
# 参数h表示预测的时间步长
# 参数time_col表示时间列的名称
# 参数target_col表示目标列的名称
# 参数freq表示数据的频率
timegpt_fcst_df = timegpt.forecast(df=df, h=6, time_col='timestamp', target_col='value', freq='MS')

# 使用timegpt模块的plot函数绘制原始数据和预测数据的图形
# 参数df表示原始数据框
# 参数timegpt_fcst_df表示预测数据框
# 参数time_col表示时间列的名称
# 参数target_col表示目标列的名称
timegpt.plot(df, timegpt_fcst_df, time_col='timestamp', target_col='value')
INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
INFO:nixtlats.timegpt:Calling Forecast Endpoint...

TimeGPT-1 is currently optimized for short-term forecasting. Although forecastthe method can allow any positive number and a large prediction range, the accuracy of the predictions may be reduced. We are currently working to improve the accuracy of our long-term forecasts.

Use DateTime index to infer frequency.

The freq parameter represents the time unit between consecutive data points, which is particularly critical. Fortunately, you can pass a DataFrame with a DateTime index to the forecast method, ensuring that your time series data is equipped with the necessary temporal characteristics. By assigning the appropriate freq parameter to the DataFrame's DateTime index, you can provide the model with information about consistent intervals between observations, whether that be days ('D'), months ('M'), or other appropriate frequencies.

# 将DataFrame的索引设置为'timestamp'列
df_time_index = df.set_index('timestamp')

# 将索引转换为DatetimeIndex对象,并设置频率为'MS'(月初)
df_time_index.index = pd.DatetimeIndex(df_time_index.index, freq='MS')

# 使用timegpt模型对DataFrame进行预测,预测未来36个时间点的值
# 预测使用的数据为df,时间列为'timestamp',目标列为'value'
forecast_result = timegpt.forecast(df=df, h=36, time_col='timestamp', target_col='value')
INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
INFO:nixtlats.timegpt:Inferred freq: MS
WARNING:nixtlats.timegpt:The specified horizon "h" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.
INFO:nixtlats.timegpt:Calling Forecast Endpoint...
timestamp TimeGPT
0 1961-01-01 437.837952
1 1961-02-01 426.062744
2 1961-03-01 463.116577
3 1961-04-01 478.244507
4 1961-05-01 505.646484

Guess you like

Origin blog.csdn.net/wjjc1017/article/details/135225996