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¶
Sktime models can be passed in the forecasters argument when initializing the TimeCopilot agent where they will be wrapped in an adapter with an alias based on the type name.
If multiple sktime forecasters of the same type are passed, each model after the first will have be wrapped in an adapter with an alias that has '_n' appended to it with n being incremented by 1 for each additional occurrence of the same model type.
For example, if you pass two TrendForecaster sktime models, the first one will have an alias of 'sktime.TrendForecaster' and the second one will have an alias of 'sktime.TrendForecaster_2'.
If you would rather specify the alias yourself, you will need to adapt the model manually with SKTimeAdapter.
from sktime.forecasting.trend import TrendForecaster
trend_forecaster = TrendForecaster()
Manually adapt sktime model¶
If you would rather decide on the alias yourself, you will need to manually adapt the model with SKTimeAdapter.
The model argument should be an sktime Forecaster model. The alias argument should be a string that uniquely identifies the model.
After adapting the model you would pass it in the forecasters argument when initializing the TimeCopilot agent.
If you add multiple manually adapted sktime models of the same type without specifying aliases, TimeCopilot may 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()
manually_adapted_model = SKTimeAdapter(
model=trend_forecaster,
alias="TrendForecaster",
)
tc = timecopilot.TimeCopilot(
llm="openai:gpt-4o",
forecasters=[
manually_adapted_model
]
)
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=[
trend_forecaster,
],
)
Extending default model list with an sktime 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(trend_forecaster)
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.52it/s] 1it [00:00, 220.94it/s] 11it [00:00, 83.97it/s]
print(result.output.tsfeatures_analysis)
The 'AirPassengers' data exhibits a strong trend with a stability of 0.933, indicating a consistent pattern over time. It also has a very strong seasonal component (seasonal_strength 0.982) with a seasonal period of 12 months, typical for annual data. The high x_acf1 (0.948) indicates strong autocorrelation, implying that past values are highly predictive of future values, suggesting that a time series model with a trend and seasonal structure would be appropriate. The entropy of the series is relatively low (0.429), indicating a high level of predictability, which substantiates the use of deterministic trend models. These features warrant the use of models that incorporate seasonality and trends, such as Holt's linear trend method or even seasonal naive models.
result.fcst_df
| unique_id | ds | sktime.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 |