mlforecast.forecast
MLForecast
__init__
models
(regressor or list of regressors): Models that will be trained and used to compute the forecasts.freq
(str or int or pd.offsets.BaseOffset): Pandas offset, pandas offset alias, e.g. ‘D’, ‘W-THU’ or integer denoting the frequency of the series.lags
(list of int, optional): Lags of the target to use as features. Defaults to None.lag_transforms
(dict of int to list of functions, optional): Mapping of target lags to their transformations. Defaults to None.date_features
(list of str or callable, optional): Features computed from the dates. Can be pandas date attributes or functions that will take the dates as input. Defaults to None.num_threads
(int): Number of threads to use when computing the features. Defaults to 1.target_transforms
(list of transformers, optional): Transformations that will be applied to the target before computing the features and restored after the forecasting step. Defaults to None.lag_transforms_namer
(callable, optional): Function that takes a transformation (either function or class), a lag and extra arguments and produces a name. Defaults to None.cross_validation
n_windows
splits where each window has h
test periods, trains the models, computes the predictions and merges the actuals.
Args:
df
(pandas or polars DataFrame): Series data in long format.n_windows
(int): Number of windows to evaluate.h
(int): Forecast horizon.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’.step_size
(int, optional): Step size between each cross validation window. If None it will be equal to h
. Defaults to None.static_features
(list of str, optional): Names of the features that are static and will be repeated when forecasting. Defaults to None.dropna
(bool): Drop rows with missing values produced by the transformations. Defaults to True.keep_last_n
(int, optional): Keep only these many records from each serie for the forecasting step. Can save time and memory if your features allow it. Defaults to None.max_horizon
(int, optional): Train this many models, where each model will predict a specific horizon. 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 True.before_predict_callback
(callable, optional): Function to call on the features before computing the predictions. This function will take the input dataframe that will be passed to the model for predicting and should return a dataframe with the same structure. The series identifier is on the index. Defaults to None.after_predict_callback
(callable, optional): Function to call on the predictions before updating the targets. This function will take a pandas Series with the predictions and should return another one with the same structure. The series identifier is on the index. Defaults to None.prediction_intervals
(PredictionIntervals, optional): Configuration to calibrate prediction intervals (Conformal Prediction). Defaults to None.level
(list of ints or floats, optional): Confidence levels between 0 and 100 for prediction intervals. 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.fitted
(bool): Store the in-sample predictions. Defaults to False.as_numpy
(bool): Cast features to numpy array. Defaults to False.weight_col
(str, optional): Column that contains the sample weights. Defaults to None.pandas or polars DataFrame
: Predictions for each window with the series id, timestamp, last train date, target value and predictions from each model.cross_validation_fitted_values
fit
df
(pandas or polars DataFrame): Series data in long format.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’.static_features
(list of str, optional): Names of the features that are static and will be repeated when forecasting. If None
, will consider all columns (except id_col and time_col) as static. Defaults to None.dropna
(bool): Drop rows with missing values produced by the transformations. Defaults to True.keep_last_n
(int, optional): Keep only these many records from each serie for the forecasting step. Can save time and memory if your features allow it. Defaults to None.max_horizon
(int, optional): Train this many models, where each model will predict a specific horizon. Defaults to None.prediction_intervals
(PredictionIntervals, optional): Configuration to calibrate prediction intervals (Conformal Prediction). Defaults to None.fitted
(bool): Save in-sample predictions. Defaults to False.as_numpy
(bool): Cast features to numpy array. Defaults to False.weight_col
(str, optional): Column that contains the sample weights. Defaults to None.MLForecast
: Forecast object with series values and trained models.fit_models
MLForecast.preprocess
beforehand.
Args:
X
(pandas or polars DataFrame or numpy array): Features.y
(numpy array): Target.MLForecast
: Forecast object with trained models.forecast_fitted_values
level
(list of ints or floats, optional): Confidence levels between 0 and 100 for prediction intervals. Defaults to None.pandas or polars DataFrame
: Dataframe with predictions for the training setfrom_cv
get_missing_future
X_df
.
Args:
h
(int): Number of periods to predict.X_df
(pandas or polars DataFrame): Dataframe with the future exogenous features. Should have the id column and the time column.pandas or polars DataFrame
: DataFrame with expected ids and future times missing in X_df
load
path
(str or pathlib.Path): Directory with saved artifacts.make_future_dataframe
h
(int): Number of periods to predict.pandas or polars DataFrame
: DataFrame with expected ids and future timespredict
h
steps.
Args:
h
(int): Number of periods to predict.before_predict_callback
(callable, optional): Function to call on the features before computing the predictions. This function will take the input dataframe that will be passed to the model for predicting and should return a dataframe with the same structure. The series identifier is on the index. Defaults to None.after_predict_callback
(callable, optional): Function to call on the predictions before updating the targets. This function will take a pandas Series with the predictions and should return another one with the same structure. The series identifier is on the index. Defaults to None.new_df
(pandas or polars DataFrame, optional): Series data of new observations for which forecasts are to be generated. This dataframe should have the same structure as the one used to fit the model, including any features and time series data. If new_df
is not None, the method will generate forecasts for the new observations. Defaults to None.level
(list of ints or floats, optional): Confidence levels between 0 and 100 for prediction intervals. Defaults to None.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.ids
(list of str, optional): List with subset of ids seen during training for which the forecasts should be computed. Defaults to None.pandas or polars DataFrame
: Predictions for each serie and timestep, with one column per model.preprocess
data
.
Args:
df
(pandas DataFrame): Series data in long format.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’.static_features
(list of str, optional): Names of the features that are static and will be repeated when forecasting. Defaults to None.dropna
(bool): Drop rows with missing values produced by the transformations. Defaults to True.keep_last_n
(int, optional): Keep only these many records from each serie for the forecasting step. Can save time and memory if your features allow it. Defaults to None.max_horizon
(int, optional): Train this many models, where each model will predict a specific horizon. Defaults to None.return_X_y
(bool): Return a tuple with the features and the target. If False will return a single dataframe. Defaults to False.as_numpy
(bool): Cast features to numpy array. Only works for return_X_y=True
. Defaults to False.weight_col
(str, optional): Column that contains the sample weights. Defaults to None.DataFrame or tuple of pandas Dataframe and a numpy array
: df
plus added features and target(s).save
path
(str or pathlib.Path): Directory where artifacts will be stored.update
df
(pandas or polars DataFrame): Dataframe with new observations.