TimeCopilot Agent¶
In [9]:
Copied!
import nest_asyncio
nest_asyncio.apply()
import nest_asyncio
nest_asyncio.apply()
Import libraries¶
In [10]:
Copied!
import pandas as pd
from timecopilot import TimeCopilot
import pandas as pd
from timecopilot import TimeCopilot
Load the dataset.¶
The DataFrame must include at least the following columns:
- unique_id: Unique identifier for each time series (string)
- ds: Date column (datetime format)
- y: Target variable for forecasting (float format)
The pandas frequency will be inferred from the ds column, if not provided. If the seasonality is not provided, it will be inferred based on the frequency. If the horizon is not set, it will default to 2 times the inferred seasonality.
In [11]:
Copied!
df = pd.read_csv("https://timecopilot.s3.amazonaws.com/public/data/air_passengers.csv")
df.head()
df = pd.read_csv("https://timecopilot.s3.amazonaws.com/public/data/air_passengers.csv")
df.head()
Out[11]:
unique_id | ds | y | |
---|---|---|---|
0 | AirPassengers | 1949-01-01 | 112 |
1 | AirPassengers | 1949-02-01 | 118 |
2 | AirPassengers | 1949-03-01 | 132 |
3 | AirPassengers | 1949-04-01 | 129 |
4 | AirPassengers | 1949-05-01 | 121 |
Initialize the forecasting agent. You can use any LLM by specifying the llm parameter.
In [12]:
Copied!
tc = TimeCopilot(
llm="openai:gpt-4o",
retries=3,
)
tc = TimeCopilot(
llm="openai:gpt-4o",
retries=3,
)
Generate forecast¶
You can optionally specify the following parameters:
- freq: The frequency of your data (e.g., 'D' for daily, 'M' for monthly)
- h: The forecast horizon, which is the number of periods to predict
- seasonality: The seasonal period of your data, which can be inferred if not provided
In [13]:
Copied!
result = tc.forecast(df=df)
result = tc.forecast(df=df)
1it [00:00, 8.31it/s] 1it [00:00, 7.24it/s] 1it [00:05, 5.77s/it] 1it [00:00, 17.29it/s] 1it [00:00, 188.78it/s] 1it [00:00, 144.74it/s] 1it [00:00, 201.90it/s] 1it [00:00, 203.62it/s] 1it [00:00, 144.84it/s] 1it [00:00, 211.99it/s] 0it [00:00, ?it/s]Importing plotly failed. Interactive plots will not work. 16:33:10 - cmdstanpy - INFO - Chain [1] start processing 16:33:10 - cmdstanpy - INFO - Chain [1] done processing 1it [00:03, 3.86s/it]
Ask a question about the future¶
In [14]:
Copied!
answer = tc.query("Which model performed best?")
print(answer.output)
answer = tc.query("Which model performed best?")
print(answer.output)
The model that performed best is the one with the lowest MASE (Mean Absolute Scaled Error) score. According to the evaluation results: - AutoCES has the lowest MASE score of 0.6225634591577892. This indicates that the AutoCES model provided the most accurate forecasts relative to the other models tested, as a lower MASE score signifies better performance.