grid to search (param_grid)and the cross-validation strategy we want to use (sayfive-fold stratified cross-validation)in[ ]from sklearn model_selection import gridsearchcv from sklearn svm import svc grid_search gridsearchcv(svc()param_gridcv= gridsearchcv will use cross-validation in place of the split into training and validation set that we used before howeverwe still need to split the data into training and test setto avoid overfitting the parametersin[ ]x_trainx_testy_trainy_test train_test_splitiris datairis targetrandom_state= the grid_search object that we created behaves just like classifierwe can call the standard methods fitpredictand score on it howeverwhen we call fitit will run cross-validation for each combination of parameters we specified in param_gridin[ ]grid_search fit(x_trainy_trainfitting the gridsearchcv object not only searches for the best parametersbut also automatically fits new model on the whole training dataset with the parameters that yielded the best cross-validation performance what happens in fit is therefore equivalent to the result of the in[ code we saw at the beginning of this section the gridsearchcv class provides very convenient interface to access the retrained model using the predict and score methods to evaluate how well the best found parameters generalizewe can call score on the test setin[ ]print("test set score{ }format(grid_search score(x_testy_test))out[ ]test set score choosing the parameters using cross-validationwe actually found model that achieves accuracy on the test set the important thing here is that we did not use the test set to choose the parameters the parameters that were found are scored in the scikit-learn estimator that is created using another estimator is called meta-estimator gridsearchcv is the most commonly used meta-estimatorbut we will see more later model evaluation and improvement