How to run GridSearchCV with multiple values in cv

Viewed 137

I am using logistic regression on loan prediction data. I am using GridSearchCV for Hyperparameter tuning and I have been trying to find a source where I could add multiple number of values for cv. for example; I want to run my model with 3, 5, 6, 7, 10 folds. This is my code:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
parameters = {'penalty': ['l1', 'l2'],
                      'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000],
                      'solver' : ['liblinear', 'newton-cg', 'lbfgs', 'saga', 'sag'],
                      'multi_class' : ['auto'],
                      'max_iter'    : [5,15,25],
                  
                     }
s_scaled_X_train = s_scaler.fit_transform(X_train)
s_scaled_X_test = s_scaler.transform(X_test)

logmodel = GridSearchCV(LogisticRegression(), parameters, cv = 10, refit = True)

I tried to look into sources where I could find a solution on how to add more numbers in the cv value here.

logmodel = GridSearchCV(LogisticRegression(), parameters, cv = 10, refit = True)

I tried something like:

bb = [3, 5, 6, 7, 10]
cv_folds = bb

and I tried to add it to the logmodel.

logmodel = GridSearchCV(LogisticRegression(), parameters, cv = cv_folds, refit = True)

This is the error I got while running it

 TypeError: cannot unpack non-iterable int object
1 Answers

I don't think there is any in-build way in Grid Search CV to adjust the number of cv, but you can use a for-loop to check which cv is better, like this:

import warnings
warnings.filterwarnings("ignore")

cv_folds = [3, 5, 6, 7, 10]

for x in cv_folds:
    logmodel = GridSearchCV(LogisticRegression(random_state = 12), parameters, cv = x, refit = True)
    logmodel.fit(X_train, y_train)
    
    print('The best score with CV =', x, 'is', logmodel.score(X_test, y_test), 'with parameters =\n\n', logmodel.best_params_, '\n\n')

it will return output like this:

The best score with CV = 3 is 0.9824561403508771 with parameters =

 {'C': 100, 'max_iter': 15, 'multi_class': 'auto', 'penalty': 'l1', 'solver': 'liblinear'} 


The best score with CV = 5 is 0.9824561403508771 with parameters =

 {'C': 100, 'max_iter': 15, 'multi_class': 'auto', 'penalty': 'l1', 'solver': 'liblinear'} 


The best score with CV = 6 is 0.9883040935672515 with parameters =

 {'C': 1000, 'max_iter': 15, 'multi_class': 'auto', 'penalty': 'l1', 'solver': 'liblinear'} 


The best score with CV = 7 is 0.9941520467836257 with parameters =

 {'C': 100, 'max_iter': 25, 'multi_class': 'auto', 'penalty': 'l1', 'solver': 'liblinear'} 


The best score with CV = 10 is 0.9883040935672515 with parameters =

 {'C': 1000, 'max_iter': 15, 'multi_class': 'auto', 'penalty': 'l1', 'solver': 'liblinear'}

We can see that CV = 7 is giving very good results.

Related