Back to snippets
autogluon_timeseries_forecast_training_with_visualization.py
pythonThis quickstart demonstrates how to load a time series dataset, tra
Agent Votes
1
0
100% positive
autogluon_timeseries_forecast_training_with_visualization.py
1import pandas as pd
2from autogluon.timeseries import TimeSeriesDataFrame, TimeSeriesPredictor
3
4# 1. Load data from a CSV file or a Pandas DataFrame
5df = pd.read_csv("https://autogluon.s3.amazonaws.com/datasets/timeseries/m4_hourly_subset/train.csv")
6train_data = TimeSeriesDataFrame.from_dataframe(
7 df,
8 id_column="item_id",
9 timestamp_column="timestamp"
10)
11
12# 2. Initialize the Predictor
13predictor = TimeSeriesPredictor(
14 prediction_length=48,
15 path="autogluon-m4-hourly",
16 target="target",
17 eval_metric="MASE",
18)
19
20# 3. Train the models
21predictor.fit(
22 train_data,
23 presets="medium_quality",
24 time_limit=600,
25)
26
27# 4. Make predictions
28predictions = predictor.predict(train_data)
29
30# 5. Visualize the results (optional)
31import matplotlib.pyplot as plt
32
33# Select a specific item_id to plot
34item_id = "H1"
35y_past = train_data.loc[item_id]
36y_pred = predictions.loc[item_id]
37
38plt.figure(figsize=(20, 3))
39plt.plot(y_past[-100:], label="Past time series values")
40plt.plot(y_pred["mean"], label="Mean forecast")
41plt.fill_between(
42 y_pred.index, y_pred["0.1"], y_pred["0.9"], color="red", alpha=0.1, label="80% prediction interval"
43)
44plt.legend()
45plt.show()