Cross Validation

Cross Validation

def cross_val_models(X, y, model_dict, cv=5, scoring='neg_mean_squared_error'):
    """
    For the scoring metrics, use the following reference
    https://scikit-learn.org/stable/modules/model_evaluation.html
    """
    res = list()
    for name in model_dict.keys():
        scores = cross_val_score(model_dict[name], X, y, cv=cv, 
                                 scoring=scoring)
        res.append((name, scores.mean(), scores.std()))
    return pd.DataFrame.from_records(res, columns=["Model Name", "CV Mean", "CV Std"])
model_dict = {
    "lightGBM Base": lgb_base,
    "lightGBM Bayes Optimization": lgb_bo,
    "lightGBM Grid Search": lgb_grid_search
}

cross_val_models(X, y, model_dict)
Previous
Next