[Python] Hyperparameter Optimisation

Discussão em 'Python' iniciado por Stack, Abril 1, 2025.

  1. Stack

    Stack Membro Participativo

    Im trying to forecast a time series using prophet model in python, for which I would like to find the optimal tuning parameters (like changepoint_range, changepoint_prior_scale, seasonality_prior_scale, holiday_prior_scale, holiday_mode) for the time series by minimising the error (MAPE) in test and train validation method and predict the future.

    def mape(y_true, y_pred):
    return round(np.mean(np.abs(((y_true-y_pred)/y_true)*100)),2)


    Currently, I am using the optuna and tuner libraries but it only takes number (trials 50; 10 iterations and 50 samples) and variation in account, but I would like to consider all the parameter variations in account to provide the optimal tuning.

    def objective(trial):
    params={'changepoint_range': trial.suggest_loguniform('changepoint_range', 0.1, 1),
    'changepoint_prior_scale' : trial.suggest_loguniform('changepoint_prior_scale', 0.01, 0.5),
    'seasonality_prior_scale': trial.suggest_loguniform('seasonality_prior_scale', 0.01, 10),
    'holidays_prior_scale': trial.suggest_loguniform('holidays_prior_scale', 0.01, 10),
    'holidays_mode': trial.suggest_categorical('holidays_mode', ['additive', 'multiplicative'])
    }
    model = Prophet()
    model.fit(Train)
    future= model.make_future_dataframe(periods= tst)
    forecast= model.predict(future)
    prediction= forecast.tail(tst)
    return mape(Test['y'], prediction['yhat'])
    study = optuna.create_study(direction='minimize')
    study.optimize(objective, n_trials=50)

    def objective_function(args_list):
    global Train, Test

    params_evaluated= []
    results= []

    for params in args_list:
    try:
    model = Prophet()
    model.fit(Train)
    future= model.make_future_dataframe(periods= tst)
    forecast= model.predict(future)
    prediction= forecast.tail(tst)
    error= mape(Test['y'], prediction['yhat'])

    params_evaluated.append(params)
    results.append(error)
    except Exception as e:
    print(f"Error for params {params}: {e}")
    params_evaluated.append(params)
    results.append(25)

    return params_evaluated, results

    from scipy.stats import uniform
    params_space= dict(changepoint_range= uniform(0.5,0.5),
    changepoint_prior_scale= uniform(0.001,0.5),
    seasonality_prior_scale= uniform(0.01,10),
    holidays_prior_scale= uniform(0.01, 10),
    holidays_mode= ['additive', 'multiplicative']
    )

    conf_dict= dict()
    conf_dict['initial_random']= 10
    conf_dict['num_iteration']= 50
    tuner= Tuner(params_space, objective_function, conf_dict)
    results= tuner.minimize()

    Continue reading...

Compartilhe esta Página