Grid search with scikit-learn
At the beginning of the model selection and cross-validation chapter we tried to select the best nearest-neighbor model for the two last features of the iris dataset. We will refocus on that now with GridSearchCV
in scikit-learn.
Getting ready
First, load the last two features of the iris dataset. Split the data into training and testing sets:
from sklearn import datasets iris = datasets.load_iris() X = iris.data[:,2:] y = iris.target from sklearn.model_selection import train_test_split, cross_val_score X_train, X_test, y_train, y_test = train_test_split(X, y, stratify = y,random_state = 7)
How to do it...
- Instantiate a nearest neighbors classifier:
from sklearn.neighbors import KNeighborsClassifier knn_clf = KNeighborsClassifier()
- Prepare a parameter grid, which is necessary for a grid search. A parameter grid is a dictionary with the parameter setting you would like to try:
param_grid = {'n_neighbors': list(range(3,9,1))}
- Instantiate a grid search passing the following...