Local
Auto
source
AutoRandomForest
AutoRandomForest (config:Optional[Callable[[optuna.trial._trial.Trial],Di ct[str,Any]]]=None)
Structure to hold a model and its search space
Type | Default | Details | |
---|---|---|---|
config | Optional | None | function that takes an optuna trial and produces a configuration |
source
AutoElasticNet
AutoElasticNet (config:Optional[Callable[[optuna.trial._trial.Trial],Dict [str,Any]]]=None)
Structure to hold a model and its search space
Type | Default | Details | |
---|---|---|---|
config | Optional | None | function that takes an optuna trial and produces a configuration |
source
AutoLasso
AutoLasso (config:Optional[Callable[[optuna.trial._trial.Trial],Dict[str, Any]]]=None)
Structure to hold a model and its search space
Type | Default | Details | |
---|---|---|---|
config | Optional | None | function that takes an optuna trial and produces a configuration |
source
AutoRidge
AutoRidge (config:Optional[Callable[[optuna.trial._trial.Trial],Dict[str, Any]]]=None)
Structure to hold a model and its search space
Type | Default | Details | |
---|---|---|---|
config | Optional | None | function that takes an optuna trial and produces a configuration |
source
AutoLinearRegression
AutoLinearRegression (config:Optional[Callable[[optuna.trial._trial.Trial ],Dict[str,Any]]]=None)
Structure to hold a model and its search space
Type | Default | Details | |
---|---|---|---|
config | Optional | None | function that takes an optuna trial and produces a configuration |
source
AutoCatboost
AutoCatboost (config:Optional[Callable[[optuna.trial._trial.Trial],Dict[s tr,Any]]]=None)
Structure to hold a model and its search space
Type | Default | Details | |
---|---|---|---|
config | Optional | None | function that takes an optuna trial and produces a configuration |
source
AutoXGBoost
AutoXGBoost (config:Optional[Callable[[optuna.trial._trial.Trial],Dict[st r,Any]]]=None)
Structure to hold a model and its search space
Type | Default | Details | |
---|---|---|---|
config | Optional | None | function that takes an optuna trial and produces a configuration |
source
AutoLightGBM
AutoLightGBM (config:Optional[Callable[[optuna.trial._trial.Trial],Dict[s tr,Any]]]=None)
Structure to hold a model and its search space
Type | Default | Details | |
---|---|---|---|
config | Optional | None | function that takes an optuna trial and produces a configuration |
source
random_forest_space
random_forest_space (trial:optuna.trial._trial.Trial)
source
elastic_net_space
elastic_net_space (trial:optuna.trial._trial.Trial)
source
lasso_space
lasso_space (trial:optuna.trial._trial.Trial)
source
ridge_space
ridge_space (trial:optuna.trial._trial.Trial)
source
linear_regression_space
linear_regression_space (trial:optuna.trial._trial.Trial)
source
catboost_space
catboost_space (trial:optuna.trial._trial.Trial)
source
xgboost_space
xgboost_space (trial:optuna.trial._trial.Trial)
source
lightgbm_space
lightgbm_space (trial:optuna.trial._trial.Trial)
source
AutoModel
AutoModel (model:sklearn.base.BaseEstimator, config:Callable[[optuna.trial._trial.Trial],Dict[str,Any]])
Structure to hold a model and its search space
Type | Details | |
---|---|---|
model | BaseEstimator | scikit-learn compatible regressor |
config | Callable | function that takes an optuna trial and produces a configuration |
source
AutoMLForecast
AutoMLForecast (models:Union[List[__main__.AutoModel],Dict[str,__main__. AutoModel]], freq:Union[int,str], season_length:int, init _config:Optional[Callable[[optuna.trial._trial.Trial],Dic t[str,Any]]]=None, fit_config:Optional[Callable[[optuna.t rial._trial.Trial],Dict[str,Any]]]=None, num_threads:int=1)
Hyperparameter optimization helper
Type | Default | Details | |
---|---|---|---|
models | Union | Auto models to be optimized. | |
freq | Union | pandas’ or polars’ offset alias or integer denoting the frequency of the series. | |
season_length | int | Length of the seasonal period. This is used for producing the feature space. | |
init_config | Optional | None | Function that takes an optuna trial and produces a configuration passed to the MLForecast constructor. |
fit_config | Optional | None | Function that takes an optuna trial and produces a configuration passed to the MLForecast fit method. |
num_threads | int | 1 | Number of threads to use when computing the features. |
source
AutoMLForecast.fit
AutoMLForecast.fit (df:Union[pandas.core.frame.DataFrame,polars.datafram e.frame.DataFrame], n_windows:int, h:int, num_samples:int, refit:Union[bool,int]=False, loss:Op tional[Callable[[Union[pandas.core.frame.DataFrame,po lars.dataframe.frame.DataFrame],Union[pandas.core.fra me.DataFrame,polars.dataframe.frame.DataFrame]],float ]]=None, id_col:str='unique_id', time_col:str='ds', target_col:str='y', study_kwargs:Optional[Dict[str,Any]]=None, optimize_kwargs:Optional[Dict[str,Any]]=None)
Carry out the optimization process. Each model is optimized independently and the best one is trained on all data
Type | Default | Details | |
---|---|---|---|
df | Union | Series data in long format. | |
n_windows | int | Number of windows to evaluate. | |
h | int | Forecast horizon. | |
num_samples | int | Number of trials to run | |
refit | Union | False | Retrain model for each cross validation window. If False, the models are trained at the beginning and then used to predict each window. If positive int, the models are retrained every refit windows. |
loss | Optional | None | Function that takes the validation and train dataframes and produces a float. If None will use the average SMAPE across series. |
id_col | str | unique_id | Column that identifies each serie. |
time_col | str | ds | Column that identifies each timestep, its values can be timestamps or integers. |
target_col | str | y | Column that contains the target. |
study_kwargs | Optional | None | Keyword arguments to be passed to the optuna.Study constructor. |
optimize_kwargs | Optional | None | Keyword arguments to be passed to the optuna.Study.optimize method. |
Returns | AutoMLForecast | object with best models and optimization results |
source
AutoMLForecast.predict
AutoMLForecast.predict (h:int, X_df:Union[pandas.core.frame.DataFrame,pol ars.dataframe.frame.DataFrame,NoneType]=None)
“Compute forecasts
Type | Default | Details | |
---|---|---|---|
h | int | Number of periods to predict. | |
X_df | Union | None | Dataframe with the future exogenous features. Should have the id column and the time column. |
Returns | Union | Predictions for each serie and timestep, with one column per model. |
source
AutoMLForecast.save
AutoMLForecast.save (path:Union[str,pathlib.Path])
Save AutoMLForecast objects
Type | Details | |
---|---|---|
path | Union | Directory where artifacts will be stored. |
Returns | None |
from datasetsforecast.m4 import M4, M4Evaluation, M4Info
from sklearn.linear_model import Ridge
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder
def train_valid_split(group):
df, *_ = M4.load(directory='data', group=group)
df['ds'] = df['ds'].astype('int')
horizon = M4Info[group].horizon
valid = df.groupby('unique_id').tail(horizon).copy()
train = df.drop(valid.index).reset_index(drop=True)
return train, valid
ridge_pipeline = make_pipeline(
ColumnTransformer(
[('encoder', OneHotEncoder(), ['unique_id'])],
remainder='passthrough',
),
Ridge()
)
auto_ridge = AutoModel(ridge_pipeline, lambda trial: {f'ridge__{k}': v for k, v in ridge_space(trial).items()})
optuna.logging.set_verbosity(optuna.logging.ERROR)
group = 'Weekly'
train, valid = train_valid_split(group)
train['unique_id'] = train['unique_id'].astype('category')
valid['unique_id'] = valid['unique_id'].astype(train['unique_id'].dtype)
info = M4Info[group]
h = info.horizon
season_length = info.seasonality
auto_mlf = AutoMLForecast(
freq=1,
season_length=season_length,
models={
'lgb': AutoLightGBM(),
'ridge': auto_ridge,
},
fit_config=lambda trial: {'static_features': ['unique_id']},
num_threads=2,
)
auto_mlf.fit(
df=train,
n_windows=2,
h=h,
num_samples=2,
optimize_kwargs={'timeout': 60},
)
auto_mlf.predict(h)
unique_id | ds | lgb | ridge | |
---|---|---|---|---|
0 | W1 | 2180 | 35529.435224 | 36110.921202 |
1 | W1 | 2181 | 35521.764894 | 36195.175757 |
2 | W1 | 2182 | 35537.417268 | 36107.528852 |
3 | W1 | 2183 | 35538.058206 | 36027.139248 |
4 | W1 | 2184 | 35614.611211 | 36092.858489 |
… | … | … | … | … |
4662 | W99 | 2292 | 15071.536978 | 15319.146221 |
4663 | W99 | 2293 | 15058.145278 | 15299.549555 |
4664 | W99 | 2294 | 15042.493434 | 15271.744712 |
4665 | W99 | 2295 | 15042.144846 | 15250.070504 |
4666 | W99 | 2296 | 15038.729044 | 15232.127800 |