Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Provide gridsearch support for time series #22

Open
SamVanhoutte opened this issue Apr 27, 2020 · 0 comments
Open

Provide gridsearch support for time series #22

SamVanhoutte opened this issue Apr 27, 2020 · 0 comments
Labels
feature All issues related to new features

Comments

@SamVanhoutte
Copy link
Contributor

See how to leverage this method (I once built) to gridsearch time series prediction algorithms (which are not compatible with GridSearchCV).

Starting from this code:

def GridSearchModel(model, fit_params: dict, 
                    X_fit: pd.Series, y_fit: pd.Series, y_val: pd.Series, horizon:int,
                    ctor_params: dict = None, validation_method = None, verbose=False):
    __best_error_score = 10000000
    __best_params = {}
    __best_predictions = None
    __best_fitted_model = None
    
    y_val = np.array(y_val)
    
    param_combinations = list(product(*(fit_params[par_name] for par_name in fit_params)))
    # Looping through all constructor possibilities
    for param_combination in param_combinations:
        kwargs = dict()
        for par_idx, param_name in enumerate(fit_params):
            kwargs[param_name] = param_combination[par_idx]
        if verbose:
            print('Applying parameter set', kwargs)
        model_fit = model.fit(**kwargs)
        __current_error_score = 0
        __y_pred = np.array(model_fit.predict(len(X_fit), len(X_fit) + horizon - 1))
        if validation_method == None or validation_method.lower() == 'mape':
            # Mean absolute percentage error (MAPE)
            __current_error_score += np.mean(np.abs((y_val - __y_pred) / y_val)) * 100
        elif validation_method == None or validation_method.lower() == 'mae':
            # Mean absolute error (MAE)
            __current_error_score += np.mean(abs(y_val-__y_pred))
        elif validation_method == None or validation_method.lower() == 'rmse':
            # Root mean squared error (RMSE)
            __current_error_score += math.sqrt(np.mean(y_val-__y_pred)**2)
        
        if __current_error_score < __best_error_score:
            print(f'Better score of {__current_error_score} with parameters', kwargs)
            __best_error_score = __current_error_score
            __best_params = kwargs
            __best_predictions = __y_pred
            __best_fitted_model = model_fit
    
    return __best_fitted_model, __best_params, __best_predictions, __best_error_score
@SamVanhoutte SamVanhoutte added the feature All issues related to new features label Apr 27, 2020
@SamVanhoutte SamVanhoutte self-assigned this Apr 27, 2020
@SamVanhoutte SamVanhoutte removed their assignment Dec 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature All issues related to new features
Projects
None yet
Development

No branches or pull requests

1 participant