Using sktime models¶
This is an example for using sktime based models with the timecopilot library.
imports¶
import nest_asyncio
nest_asyncio.apply()
import timecopilot
import sktime as skt
import pandas as pd
Setup the sktime model and adapt it to TimeCopilot¶
sktime models need to be adapted to work properly with TimeCopilot. This is done by creating your model with sktime and passing it through SKTimeAdapter. Some sktime models may require more configuration to function properly with the data you intend to use it on. For example, when using sktime's NaiveForecaster with yearly data you might want to initialize it with an sp argument of 12 like this NaiveForecaster(sp=12).
The Alias argument should also be provided, especially if you plan on adding multiple sktime forecasters. If you add multiple sktime models without specifying aliases, TimeCopilot will not be able to properly call all of them.
from sktime.forecasting.trend import TrendForecaster
from timecopilot.models.adapters.sktime import SKTimeAdapter
trend_forecaster = TrendForecaster()
adapted_model = SKTimeAdapter(
model=trend_forecaster,
alias="TrendForecaster",
)
Create a TimeCopilot instance with your sktime model¶
You will need to specify the forecasters you're using when using sktime models.
tc = timecopilot.TimeCopilot(
llm="openai:gpt-4o",
forecasters=[
adapted_model,
],
)
Extending default model list with an sktime adapted model¶
if you want to use the default list with the addition of your sktime model you could make a copy of the default list and append your model to it:
model_list = timecopilot.agent.DEFAULT_MODELS.copy()
model_list.append(adapted_model)
tc = timecopilot.TimeCopilot(
llm="openai:gpt-4o",
forecasters=model_list
)
Forecasting¶
Once that setup is complete, you can use TimeCopilot with your adapted sktime model the same way you'd normally use TimeCopilot
df = pd.read_csv("https://timecopilot.s3.amazonaws.com/public/data/air_passengers.csv")
result = tc.forecast(
df=df,
)
1it [00:00, 4.70it/s] 1it [00:00, 223.32it/s] 11it [00:00, 77.11it/s]
print(result.output.tsfeatures_analysis)
The 'AirPassengers' time series has a series length of 144 with a clear seasonal pattern identified using key features. The high 'seasonal_strength' of 0.981 suggests strong seasonality, evident from the 12-month seasonal period. The time series also exhibits trends, shown by a 'trend' score of 0.997, and moderate curvature at 1.069. The high autocorrelation 'x_acf1' at 0.948 indicates the persistence of patterns over time. The Holt-Winters parameters suggest a stable level (alpha ~1) with no trend component (beta ~0) and significant seasonal smoothing (gamma ~0.75). These features suggest that both trend and seasonality are prominent and need to be captured by the model.
result.fcst_df
| unique_id | ds | TrendForecaster | |
|---|---|---|---|
| 0 | AirPassengers | 1961-01-01 | 473.023018 |
| 1 | AirPassengers | 1961-02-01 | 475.729097 |
| 2 | AirPassengers | 1961-03-01 | 478.173296 |
| 3 | AirPassengers | 1961-04-01 | 480.879374 |
| 4 | AirPassengers | 1961-05-01 | 483.498159 |
| 5 | AirPassengers | 1961-06-01 | 486.204237 |
| 6 | AirPassengers | 1961-07-01 | 488.823023 |
| 7 | AirPassengers | 1961-08-01 | 491.529101 |
| 8 | AirPassengers | 1961-09-01 | 494.235179 |
| 9 | AirPassengers | 1961-10-01 | 496.853964 |
| 10 | AirPassengers | 1961-11-01 | 499.560042 |
| 11 | AirPassengers | 1961-12-01 | 502.178827 |
| 12 | AirPassengers | 1962-01-01 | 504.884906 |
| 13 | AirPassengers | 1962-02-01 | 507.590984 |
| 14 | AirPassengers | 1962-03-01 | 510.035183 |
| 15 | AirPassengers | 1962-04-01 | 512.741261 |
| 16 | AirPassengers | 1962-05-01 | 515.360046 |
| 17 | AirPassengers | 1962-06-01 | 518.066125 |
| 18 | AirPassengers | 1962-07-01 | 520.684910 |
| 19 | AirPassengers | 1962-08-01 | 523.390988 |
| 20 | AirPassengers | 1962-09-01 | 526.097066 |
| 21 | AirPassengers | 1962-10-01 | 528.715851 |
| 22 | AirPassengers | 1962-11-01 | 531.421929 |
| 23 | AirPassengers | 1962-12-01 | 534.040714 |