Here's a completed version of the function:
import pandas as pd
def evaluate_model_complete(
df, model_list=[CrostonClassic(),CrostonOptimized(),ADIDA(),IMAPA()], season_length=1, horizon=180, step_size=180, n_windows=18, freq='D'
):
"""
Evaluate multiple time series forecasting models on a given dataset.
Parameters:
- df (DataFrame): Input dataset with 'unique_id' and 'ds' columns.
- model_list (list): List of forecasting models to evaluate.
- season_length (int): Season length for the models.
- horizon (int): Forecasting horizon.
- step_size (int): Step size for the evaluation.
- n_windows (int): Number of evaluation windows.
- freq (str): Frequency of the data.
Returns:
- res (dict): Dictionary with average MAE, MASE, and sMAPE for each model.
"""
d_eval = {m.__class__.__name__:[] for m in model_list}
# Pour chaque article, faire évaluation
for art_id in df['unique_id'].unique():
df_art = df[df['unique_id'] == art_id]
if get_zero_ratio(df_art) > 0.2: # Si trop de zéros au début, ne pas évaluer modèles sur série
continue
train = df_art[df_art['ds']<='2024-06-01']
for cur_model in model_list: # Pour chaque modèle, évaluer
metrics = evaluate_model(
cur_model, df=df_art, train_df=train, n_windows=n_windows, horizon=horizon, step_size=step_size, season_length=season_length, freq=freq)
d_eval[cur_model.__class__.__name__].append(metrics[cur_model.__class__.__name__].values)
# Calculer moyenne des métriques
res = {}
for m in d_eval.keys():
# Concatenate all metric values for the current model
metrics = pd.concat([pd.Series(x) for x in d_eval[m]], ignore_index=True)
# Calculate average MAE, MASE, and sMAPE
mae = metrics.mean()
# Assuming the metrics dataframe has 'MASE' and 'sMAPE' columns
mase = evaluate_model(cur_model, df=df_art, train_df=train, n_windows=n_windows, horizon=horizon, step_size=step_size, season_length=season_length, freq=freq)['MASE'].mean()
smape = evaluate_model(cur_model, df=df_art, train_df=train, n_windows=n_windows, horizon=horizon, step_size=step_size, season_length=season_length, freq=freq)['sMAPE'].mean()
# However, the above is not efficient. A better way is to directly calculate MASE and sMAPE from the metrics dataframe
# Here, we assume that the evaluate_model function returns a dataframe with 'MAE', 'MASE', and 'sMAPE' columns
# So, we modify the above code as follows:
metrics_df = pd.concat(d_eval[m], ignore_index=True)
mae = metrics_df['MAE'].mean()
mase = metrics_df['MASE'].mean()
smape = metrics_df['sMAPE'].mean()
res[m] = {'MAE': mae, 'MASE': mase, 'sMAPE': smape}
return res
However, the above code still has some issues. The d_eval dictionary is storing the metric values for each model, but it's not clear if the evaluate_model function returns a dataframe with 'MAE', 'MASE', and 'sMAPE' columns.