ツールシリーズ:TimeGPT_(7) 過去データ予測

当社の時系列モデルは、ユーザーが将来の予測とともに過去の予測を取得できる強力な機能を提供します。この機能には、add_history=True パラメーターを設定することにより、予測メソッドを通じてアクセスできます。

# 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对象的实例

これで、予測を開始できます。例をインポートしてみましょう:

# 从指定的URL读取CSV文件,并将其存储在名为df的数据框中
df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv')

# 显示数据框的前几行
df.head()
タイムスタンプ 価値
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')

近似値を追加してみましょう。True に設定するとadd_history、出力データフレームには h パラメーターによって決定される将来の予測だけでなく、過去の予測も含まれます。現在、過去の予測はhデータ頻度の影響を受けず、データ頻度に基づいて一定の時間枠が設定されています。過去の予測はローリング ウィンドウ方式で生成され、連結されます。

# 导入所需的模块和函数

# 使用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()
タイムスタンプ 時間GPT
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

結果をプロットしてみましょう。過去と将来の予測に関するこの包括的なビューは、モデルの動作を理解し、そのパフォーマンスを長期にわたって評価するのに役立ちます。


# 使用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')

ただし、シリーズの初期値はこれらの過去の予測には含まれていないことに注意してください。TimeGPTこれは、信頼できる予測を生成するために、モデルが一定数の初期観測を必要とするためです。したがって、出力を解釈するときは、予測自体ではなく、最初のいくつかの観測値がモデルの予測の基礎として機能することを認識することが重要です。

おすすめ

転載: blog.csdn.net/wjjc1017/article/details/135244562