Tool series: TimeGPT_(7) Historical data prediction

Our time series model provides a powerful feature that allows users to retrieve historical forecasts alongside future forecasts. This functionality can be accessed through the forecast method by setting the add_history=True parameter.

# Import the colab_badge module from the nixtlats.utils package
from nixtlats.utils import colab_badge

colab_badge('docs/tutorials/7_historical_forecast')
# 导入load_dotenv函数,用于加载.env文件中的环境变量
from dotenv import load_dotenv
# 加载环境变量文件
load_dotenv()
True
# 导入pandas库
import pandas as pd
# 导入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
# 导入TimeGPT类

# 创建一个TimeGPT对象,传入一个参数token,用于身份验证
# 如果没有提供token参数,则默认使用环境变量中的TIMEGPT_TOKEN
timegpt = TimeGPT(
    token = 'my_token_provided_by_nixtla'
)
# 导入TimeGPT模型

timegpt = TimeGPT()  # 创建TimeGPT对象的实例

Now you can start making predictions! Let's import an example:

# 从指定的URL读取CSV文件,并将其存储在名为df的数据框中
df = 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
# 导入timegpt模块
import timegpt

# 使用timegpt模块的plot函数绘制图表
# 参数df为数据集,time_col为时间列,target_col为目标列
timegpt.plot(df, time_col='timestamp', target_col='value')

Let's add the fitted values. When add_historyset to True, the output DataFrame will include not only future predictions determined by the h parameter, but also historical predictions. Currently, historical forecasts are unaffected hby and have a fixed time frame based on data frequency. Historical forecasts are generated and concatenated in a rolling window fashion.

# 导入所需的模块和函数

# 使用timegpt模型对给定的数据进行预测
# 参数:
# - df: 输入的数据框,包含时间戳和目标值
# - h: 预测的时间步长
# - time_col: 时间戳所在的列名
# - target_col: 目标值所在的列名
# - add_history: 是否将历史数据添加到预测结果中
# 返回值:
# - timegpt_fcst_with_history_df: 包含预测结果和历史数据的数据框
timegpt_fcst_with_history_df = timegpt.forecast(
    df=df, h=12, time_col='timestamp', target_col='value',
    add_history=True,
)
INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
INFO:nixtlats.timegpt:Inferred freq: MS
INFO:nixtlats.timegpt:Calling Forecast Endpoint...
INFO:nixtlats.timegpt:Calling Historical Forecast Endpoint...
# 查看数据的前几行
timegpt_fcst_with_history_df.head()
timestamp TimeGPT
0 1951-01-01 135.483673
1 1951-02-01 144.442398
2 1951-03-01 157.191910
3 1951-04-01 148.769363
4 1951-05-01 140.472946

Let's plot the results. This comprehensive view of past and future predictions is valuable for understanding a model's behavior and assessing its performance over time.


# 使用timegpt模块中的plot函数绘制图表
# 参数df为原始数据集
# 参数timegpt_fcst_with_history_df为包含预测结果和历史数据的数据集
# 参数time_col指定时间列的名称
# 参数target_col指定目标列的名称
timegpt.plot(df, timegpt_fcst_with_history_df, time_col='timestamp', target_col='value')

Note, however, that the initial value of the series is not included in these historical forecasts. This is because our model TimeGPTrequires a certain number of initial observations to generate reliable predictions. Therefore, when interpreting the output, it is important to realize that the first few observations serve as the basis for the model's predictions, not the predictions themselves.

Guess you like

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