module mlforecast.auto


function lightgbm_space

lightgbm_space(trial: Trial)

function xgboost_space

xgboost_space(trial: Trial)

function catboost_space

catboost_space(trial: Trial)

function linear_regression_space

linear_regression_space(trial: Trial)

function ridge_space

ridge_space(trial: Trial)

function lasso_space

lasso_space(trial: Trial)

function elastic_net_space

elastic_net_space(trial: Trial)

function random_forest_space

random_forest_space(trial: Trial)

class AutoModel

Structure to hold a model and its search space Args:
  • model (BaseEstimator): scikit-learn compatible regressor
  • config (callable): function that takes an optuna trial and produces a configuration

method __init__

__init__(model: BaseEstimator, config: Callable[[Trial], Dict[str, Any]])

class AutoLightGBM

method __init__

__init__(config: Optional[Callable[[Trial], Dict[str, Any]]] = None)

class AutoXGBoost

method __init__

__init__(config: Optional[Callable[[Trial], Dict[str, Any]]] = None)

class AutoCatboost

method __init__

__init__(config: Optional[Callable[[Trial], Dict[str, Any]]] = None)

class AutoLinearRegression

method __init__

__init__(config: Optional[Callable[[Trial], Dict[str, Any]]] = None)

class AutoRidge

method __init__

__init__(config: Optional[Callable[[Trial], Dict[str, Any]]] = None)

class AutoLasso

method __init__

__init__(config: Optional[Callable[[Trial], Dict[str, Any]]] = None)

class AutoElasticNet

method __init__

__init__(config: Optional[Callable[[Trial], Dict[str, Any]]] = None)

class AutoRandomForest

method __init__

__init__(config: Optional[Callable[[Trial], Dict[str, Any]]] = None)

class AutoMLForecast

Hyperparameter optimization helper Args:
  • models (list or dict): Auto models to be optimized.
  • freq (str or int): pandas’ or polars’ offset alias or integer denoting the frequency of the series.
  • season_length (int, optional): Length of the seasonal period. This is used for producing the feature space. Only required if init_config is None. Defaults to None.
  • init_config (callable, optional): Function that takes an optuna trial and produces a configuration passed to the MLForecast constructor. Defaults to None.
  • fit_config (callable, optional): Function that takes an optuna trial and produces a configuration passed to the MLForecast fit method. Defaults to None.
  • num_threads (int): Number of threads to use when computing the features. Defaults to 1.

method __init__

__init__(
    models: Union[List[AutoModel], Dict[str, AutoModel]],
    freq: Union[int, str],
    season_length: Optional[int] = None,
    init_config: Optional[Callable[[Trial], Dict[str, Any]]] = None,
    fit_config: Optional[Callable[[Trial], Dict[str, Any]]] = None,
    num_threads: int = 1
)

method fit

fit(
    df: Union[DataFrame, pl_DataFrame],
    n_windows: int,
    h: int,
    num_samples: int,
    step_size: Optional[int] = None,
    input_size: Optional[int] = None,
    refit: Union[bool, int] = False,
    loss: Optional[Callable[[Union[DataFrame, pl_DataFrame], Union[DataFrame, pl_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,
    fitted: bool = False,
    prediction_intervals: Optional[PredictionIntervals] = None
) → AutoMLForecast
Carry out the optimization process. Each model is optimized independently and the best one is trained on all data Args:
  • df (pandas or polars DataFrame): 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
  • step_size (int, optional): Step size between each cross validation window. If None it will be equal to h. Defaults to None.
  • input_size (int, optional): Maximum training samples per serie in each window. If None, will use an expanding window. Defaults to None.
  • refit (bool or int): 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. Defaults to False.
  • loss (callable, optional): Function that takes the validation and train dataframes and produces a float. If None will use the average SMAPE across series. Defaults to None.
  • id_col (str): Column that identifies each serie. Defaults to ‘unique_id’.
  • time_col (str): Column that identifies each timestep, its values can be timestamps or integers. Defaults to ‘ds’.
  • target_col (str): Column that contains the target. Defaults to ‘y’.
  • study_kwargs (dict, optional): Keyword arguments to be passed to the optuna.Study constructor. Defaults to None.
  • optimize_kwargs (dict, optional): Keyword arguments to be passed to the optuna.Study.optimize method. Defaults to None.
  • fitted (bool): Whether to compute the fitted values when retraining the best model. Defaults to False.
  • prediction_intervals: Configuration to calibrate prediction intervals when retraining the best model.
Returns:
  • (AutoMLForecast): object with best models and optimization results

method forecast_fitted_values

forecast_fitted_values(
    level: Optional[List[Union[int, float]]] = None
) → Union[DataFrame, pl_DataFrame]
Access in-sample predictions. Args:
  • level (list of ints or floats, optional): Confidence levels between 0 and 100 for prediction intervals. Defaults to None.
Returns:
  • (pandas or polars DataFrame): Dataframe with predictions for the training set

method predict

predict(
    h: int,
    X_df: Optional[DataFrame, pl_DataFrame] = None,
    level: Optional[List[Union[int, float]]] = None
) → Union[DataFrame, pl_DataFrame]
“Compute forecasts Args:
  • h (int): Number of periods to predict.
  • X_df (pandas or polars DataFrame, optional): Dataframe with the future exogenous features. Should have the id column and the time column. Defaults to None.
  • level (list of ints or floats, optional): Confidence levels between 0 and 100 for prediction intervals. Defaults to None.
Returns:
  • (pandas or polars DataFrame): Predictions for each serie and timestep, with one column per model.

method save

save(path: Union[str, Path]) → None
Save AutoMLForecast objects Args:
  • path (str or pathlib.Path): Directory where artifacts will be stored.