Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the best parameters using sklearn nested cross-validation

Is my understanding correct that "greed_search.fit(X,Y)" has nothing with nested CV in the following code? Meaning that there is no way to get the best parameters using nested CV in sklearn.

    # inner cross_validation
    greed_search = GridSearchCV(estimator=estimator, param_grid=parameters, cv=inner_cv, scoring=optimized_for)
    greed_search.fit(X, optimization_label)
    # Nested CV with parameter optimization
    nested_score = cross_val_score(greed_search, X=X, y=Y, cv=outer_cv)
like image 414
E.Asgari Avatar asked Oct 18 '22 07:10

E.Asgari


1 Answers

You are right: the greed_search.fit(X, optimization_label) in your code is performed as is without being nested into the next cross validation.

To answer your second question I ask you another question: What should be the best parameters of the grid search nested into the cross validation? The ones of the first fold? the most commons?

The inner grid search, at each step of the outer cross validation, selects the best parameters according to the training data of the current step. Hence the parameters can change among the folds. Doing the outer cross validation by yourself you can compute the best parameters of each step, but I don't think you really need it.

like image 148
Roberto Trani Avatar answered Oct 20 '22 23:10

Roberto Trani