Start learning free

LESSON 62 OF 165 · MACHINE LEARNING: DOING IT RIGHT

Tune the Settings

Practice

KNN has a setting you choose yourself: n_neighbors, how many of the closest training customers get a vote. The starting code tries 5, 15 and 25 and scores each one on the test rows. 15 wins with 0.76, but if you pick it because of the test rows, 0.76 stops being an honest score: the test rows helped you choose, and the winner is partly the one that got lucky on those 75 rows.

GridSearchCV chooses with cross-validation on the training rows instead. You give it the pipeline, a dict of values to try (the grid) and cv=5. Inside a pipeline a setting is named after its step, two underscores, then the setting: the C of a logisticregression step is logisticregression__C. fit cross-validates every value, keeps the best and fits it again on all the training rows.

pipe = make_pipeline(StandardScaler(), LogisticRegression(max_iter=2000))
search = GridSearchCV(pipe, {"logisticregression__C": [0.1, 1, 10]}, cv=5)
search.fit(X_train, y_train)
print(search.best_params_)

After fit, search.cv_results_["mean_test_score"] gives each value's mean score over the folds, in the grid's order; best_params_ gives back a dict with the winning value; best_score_ is the winner's mean score; and search.score scores the best pipeline. The test rows wait for that last line. Your script must print:

n_neighbors 5 | test rows: 0.64
n_neighbors 15 | test rows: 0.76
n_neighbors 25 | test rows: 0.733
Mean score per setting: [0.724 0.76  0.782]
Best settings: {'kneighborsclassifier__n_neighbors': 25}
Best cross-validated score: 0.782
Final check on the test rows: 0.733

Cross-validation picks 25, not 15, and the honest score of the chosen model is 0.733, not 0.76.

Your task: make a grid that tries n_neighbors 5, 15 and 25 for the pipeline's KNN step, search it with GridSearchCV and cv=5 on the training rows, print the mean score per setting, the best settings and the best cross-validated score, and finish with one score on the test rows. Round the scores to 3 decimals.


Assignment
Follow the Coding Tutorial to tune n_neighbors with GridSearchCV and check the winner once on the test rows.


Hint
The pipeline's KNN step is named kneighborsclassifier, so its setting is kneighborsclassifier__n_neighbors. Fit search on X_train and y_train only.


STEP 1 OF 7
    Ctrl + Enter to check