GridSearchCV
GridsearchCV
is a method of tuning wherein the model can be built by evaluating the combination of parameters mentioned in a grid. In the following figure, we will see how GridSearchCV
is different from manual search and look at grid search in a much-detailed way in a table format.
Tuning using GridSearchCV
We can conduct a grid search much more easily in practice by leveraging model_selection.GridSearchCV
.
For the sake of comparison, we will use the same breast cancer dataset and k-NN classifier as before:
from sklearn import model_selection, datasets, neighbors # load the data cancer = datasets.load_breast_cancer() # target y = cancer.target # features X = cancer.data
The next thing we need to do after loading the data is to initialize the class of the estimator we would like to evaluate under different hyperparameterizations:
# initialize the estimator knn = neighbors.KNeighborsClassifier()
We then define the grid:
# grid contains k and...