> ## Documentation Index
> Fetch the complete documentation index at: https://nixtla-old-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Auto

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L226" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoRandomForest

> ```text theme={null}
>  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 |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L213" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoElasticNet

> ```text theme={null}
>  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 |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L200" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoLasso

> ```text theme={null}
>  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 |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L187" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoRidge

> ```text theme={null}
>  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 |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L174" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoLinearRegression

> ```text theme={null}
>  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 |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L161" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoCatboost

> ```text theme={null}
>  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 |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L148" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoXGBoost

> ```text theme={null}
>  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 |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L135" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoLightGBM

> ```text theme={null}
>  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 |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L100" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### random\_forest\_space

> ```text theme={null}
>  random_forest_space (trial:optuna.trial._trial.Trial)
> ```

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L92" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### elastic\_net\_space

> ```text theme={null}
>  elastic_net_space (trial:optuna.trial._trial.Trial)
> ```

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L85" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### lasso\_space

> ```text theme={null}
>  lasso_space (trial:optuna.trial._trial.Trial)
> ```

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L78" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### ridge\_space

> ```text theme={null}
>  ridge_space (trial:optuna.trial._trial.Trial)
> ```

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L74" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### linear\_regression\_space

> ```text theme={null}
>  linear_regression_space (trial:optuna.trial._trial.Trial)
> ```

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L62" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### catboost\_space

> ```text theme={null}
>  catboost_space (trial:optuna.trial._trial.Trial)
> ```

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L49" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### xgboost\_space

> ```text theme={null}
>  xgboost_space (trial:optuna.trial._trial.Trial)
> ```

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L34" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### lightgbm\_space

> ```text theme={null}
>  lightgbm_space (trial:optuna.trial._trial.Trial)
> ```

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L112" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoModel

> ```text theme={null}
>  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 |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L239" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoMLForecast

> ```text theme={null}
>  AutoMLForecast
>                  (models:Union[List[__main__.AutoModel],Dict[str,__main__.
>                  AutoModel]], freq:Union[int,str],
>                  season_length:Optional[int]=None, init_config:Optional[Ca
>                  llable[[optuna.trial._trial.Trial],Dict[str,Any]]]=None, 
>                  fit_config:Optional[Callable[[optuna.trial._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 | Optional | None        | Length of the seasonal period. This is used for producing the feature space.<br />Only required if `init_config` is None. |
| 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.                                                                     |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L439" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoMLForecast.fit

> ```text theme={null}
>  AutoMLForecast.fit
>                      (df:Union[pandas.core.frame.DataFrame,polars.datafram
>                      e.frame.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[pandas.core.frame.DataFrame,polars.dataframe.fr
>                      ame.DataFrame],Union[pandas.core.frame.DataFrame,pola
>                      rs.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,
>                      fitted:bool=False, prediction_intervals:Optional[mlfo
>                      recast.utils.PredictionIntervals]=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                                                                                                                                                                                            |
| step\_size            | Optional           | None        | Step size between each cross validation window. If None it will be equal to `h`.                                                                                                                                   |
| input\_size           | Optional           | None        | Maximum training samples per serie in each window. If None, will use an expanding window.                                                                                                                          |
| refit                 | Union              | False       | Retrain model for each cross validation window.<br />If False, the models are trained at the beginning and then used to predict each window.<br />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.<br />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.                                                                                                                                                |
| fitted                | bool               | False       | Whether to compute the fitted values when retraining the best model.                                                                                                                                               |
| prediction\_intervals | Optional           | None        | Configuration to calibrate prediction intervals when retraining the best model.                                                                                                                                    |
| **Returns**           | **AutoMLForecast** |             | **object with best models and optimization results**                                                                                                                                                               |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L590" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoMLForecast.predict

> ```text theme={null}
>  AutoMLForecast.predict (h:int, X_df:Union[pandas.core.frame.DataFrame,pol
>                          ars.dataframe.frame.DataFrame,NoneType]=None,
>                          level:Optional[List[Union[int,float]]]=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. |
| level       | Optional  | None        | Confidence levels between 0 and 100 for prediction intervals.                                |
| **Returns** | **Union** |             | **Predictions for each serie and timestep, with one column per model.**                      |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L622" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoMLForecast.save

> ```text theme={null}
>  AutoMLForecast.save (path:Union[str,pathlib.Path])
> ```

*Save AutoMLForecast objects*

|             | **Type** | **Details**                               |
| ----------- | -------- | ----------------------------------------- |
| path        | Union    | Directory where artifacts will be stored. |
| **Returns** | **None** |                                           |

***

<a href="https://github.com/Nixtla/mlforecast/blob/main/mlforecast/auto.py#L632" target="_blank" style={{ float: "right", fontSize: "smaller" }}>source</a>

### AutoMLForecast.forecast\_fitted\_values

> ```text theme={null}
>  AutoMLForecast.forecast_fitted_values
>                                         (level:Optional[List[Union[int,flo
>                                         at]]]=None)
> ```

*Access in-sample predictions.*

|             | **Type**  | **Default** | **Details**                                                   |
| ----------- | --------- | ----------- | ------------------------------------------------------------- |
| level       | Optional  | None        | Confidence levels between 0 and 100 for prediction intervals. |
| **Returns** | **Union** |             | **Dataframe with predictions for the training set**           |

```python theme={null}
import time

import pandas as pd
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
```

```python theme={null}
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
```

```python theme={null}
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()})
```

```python theme={null}
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},
    fitted=True,
    prediction_intervals=PredictionIntervals(n_windows=2, h=h),
)
auto_mlf.predict(h, level=[80])
```

|      | unique\_id | ds   | lgb          | lgb-lo-80    | lgb-hi-80    | ridge        | ridge-lo-80  | ridge-hi-80  |
| ---- | ---------- | ---- | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| 0    | W1         | 2180 | 35529.435224 | 35061.835362 | 35997.035086 | 36110.921202 | 35880.445097 | 36341.397307 |
| 1    | W1         | 2181 | 35521.764894 | 34973.035617 | 36070.494171 | 36195.175757 | 36051.013811 | 36339.337702 |
| 2    | W1         | 2182 | 35537.417268 | 34960.050939 | 36114.783596 | 36107.528852 | 35784.062169 | 36430.995536 |
| 3    | W1         | 2183 | 35538.058206 | 34823.640706 | 36252.475705 | 36027.139248 | 35612.635725 | 36441.642771 |
| 4    | W1         | 2184 | 35614.611211 | 34627.023739 | 36602.198683 | 36092.858489 | 35389.690977 | 36796.026000 |
| ...  | ...        | ...  | ...          | ...          | ...          | ...          | ...          | ...          |
| 4662 | W99        | 2292 | 15071.536978 | 14484.617399 | 15658.456557 | 15319.146221 | 14869.410567 | 15768.881875 |
| 4663 | W99        | 2293 | 15058.145278 | 14229.686322 | 15886.604234 | 15299.549555 | 14584.269352 | 16014.829758 |
| 4664 | W99        | 2294 | 15042.493434 | 14096.380636 | 15988.606232 | 15271.744712 | 14365.349338 | 16178.140086 |
| 4665 | W99        | 2295 | 15042.144846 | 14037.053904 | 16047.235787 | 15250.070504 | 14403.428791 | 16096.712216 |
| 4666 | W99        | 2296 | 15038.729044 | 13944.821480 | 16132.636609 | 15232.127800 | 14325.059776 | 16139.195824 |

```python theme={null}
auto_mlf.forecast_fitted_values(level=[95])
```

|        | unique\_id | ds   | y        | lgb          | lgb-lo-95    | lgb-hi-95    | ridge        | ridge-lo-95  | ridge-hi-95  |
| ------ | ---------- | ---- | -------- | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| 0      | W1         | 15   | 1071.06  | 1060.584344  | 599.618355   | 1521.550334  | 1076.990151  | 556.535492   | 1597.444810  |
| 1      | W1         | 16   | 1073.73  | 1072.669242  | 611.703252   | 1533.635232  | 1083.633276  | 563.178617   | 1604.087936  |
| 2      | W1         | 17   | 1066.97  | 1072.452128  | 611.486139   | 1533.418118  | 1084.724311  | 564.269652   | 1605.178970  |
| 3      | W1         | 18   | 1066.17  | 1065.837828  | 604.871838   | 1526.803818  | 1080.127197  | 559.672538   | 1600.581856  |
| 4      | W1         | 19   | 1064.43  | 1065.214681  | 604.248691   | 1526.180671  | 1080.636826  | 560.182167   | 1601.091485  |
| ...    | ...        | ...  | ...      | ...          | ...          | ...          | ...          | ...          | ...          |
| 361881 | W99        | 2279 | 15738.54 | 15887.661228 | 15721.237195 | 16054.085261 | 15927.918181 | 15723.222760 | 16132.613603 |
| 361882 | W99        | 2280 | 15388.13 | 15755.943789 | 15589.519756 | 15922.367823 | 15841.599064 | 15636.903642 | 16046.294485 |
| 361883 | W99        | 2281 | 15187.62 | 15432.224701 | 15265.800668 | 15598.648735 | 15584.462232 | 15379.766811 | 15789.157654 |
| 361884 | W99        | 2282 | 15172.27 | 15177.040831 | 15010.616797 | 15343.464864 | 15396.243223 | 15191.547801 | 15600.938644 |
| 361885 | W99        | 2283 | 15101.03 | 15162.090803 | 14995.666770 | 15328.514836 | 15335.982465 | 15131.287044 | 15540.677887 |

```python theme={null}
import polars as pl
```

```python theme={null}
train_pl = pl.from_pandas(train.astype({'unique_id': 'str'}))
auto_mlf = AutoMLForecast(
    freq=1,
    season_length=season_length,
    models={'ridge': AutoRidge()},
    num_threads=2,
)
auto_mlf.fit(
    df=train_pl,
    n_windows=2,
    h=h,
    num_samples=2,
    optimize_kwargs={'timeout': 60},
    fitted=True,
    prediction_intervals=PredictionIntervals(n_windows=2, h=h),
)
auto_mlf.predict(h, level=[80])
```

| unique\_id | ds   | ridge        | ridge-lo-80  | ridge-hi-80  |
| ---------- | ---- | ------------ | ------------ | ------------ |
| str        | i64  | f64          | f64          | f64          |
| "W1"       | 2180 | 35046.096663 | 34046.69521  | 36045.498116 |
| "W1"       | 2181 | 34743.269216 | 33325.847975 | 36160.690457 |
| "W1"       | 2182 | 34489.591086 | 32591.254559 | 36387.927614 |
| "W1"       | 2183 | 34270.768179 | 32076.507727 | 36465.02863  |
| "W1"       | 2184 | 34124.021857 | 31352.454121 | 36895.589593 |
| …          | …    | …            | …            | …            |
| "W99"      | 2292 | 14719.457096 | 13983.308582 | 15455.605609 |
| "W99"      | 2293 | 14631.552077 | 13928.874336 | 15334.229818 |
| "W99"      | 2294 | 14532.905239 | 13642.840118 | 15422.97036  |
| "W99"      | 2295 | 14446.065443 | 13665.088667 | 15227.04222  |
| "W99"      | 2296 | 14363.049604 | 13654.220051 | 15071.879157 |

```python theme={null}
auto_mlf.forecast_fitted_values(level=[95])
```

| unique\_id | ds   | y        | ridge        | ridge-lo-95  | ridge-hi-95  |
| ---------- | ---- | -------- | ------------ | ------------ | ------------ |
| str        | i64  | f64      | f64          | f64          | f64          |
| "W1"       | 14   | 1061.96  | 1249.326428  | 488.765249   | 2009.887607  |
| "W1"       | 15   | 1071.06  | 1246.067836  | 485.506657   | 2006.629015  |
| "W1"       | 16   | 1073.73  | 1254.027897  | 493.466718   | 2014.589076  |
| "W1"       | 17   | 1066.97  | 1254.475948  | 493.914769   | 2015.037126  |
| "W1"       | 18   | 1066.17  | 1248.306754  | 487.745575   | 2008.867933  |
| …          | …    | …        | …            | …            | …            |
| "W99"      | 2279 | 15738.54 | 15754.558812 | 15411.968645 | 16097.148979 |
| "W99"      | 2280 | 15388.13 | 15655.780865 | 15313.190698 | 15998.371032 |
| "W99"      | 2281 | 15187.62 | 15367.498468 | 15024.908301 | 15710.088635 |
| "W99"      | 2282 | 15172.27 | 15172.591423 | 14830.001256 | 15515.18159  |
| "W99"      | 2283 | 15101.03 | 15141.032886 | 14798.44272  | 15483.623053 |
