Accuracy and the Confusion Matrix
For classification, accuracy is the default.
Why the Metric Decides the Winner
Earlier evaluation work taught you to compute a metric from ground truth and a model's predictions. Model selection asks a different question: which setting of a model's parameters should you keep? To answer it, a search tool has to know which number to compare across candidate settings. That number is the scoring metric, and in scikit-learn you choose it with a single argument.
What do you think happens?
You run a grid search over gamma with the default scoring, which is accuracy for classification. Then you run the same search with scoring set to roc_auc, on the same training data. Do you expect both runs to keep the same gamma?
Reveal answer
The winner can change. In the source's run, accuracy kept gamma=0.0001 and AUC kept gamma=0.01.
The fitting and the candidate settings are the same in both runs. Only the number used to compare candidates differs, and that is enough to change which setting wins.
This lesson is about that comparison number. You will trace where the scoring argument enters the search, read a pair of runs that disagree, and learn which scoring strings to reach for. The trace starts with the workflow itself.
Tracing Where Scoring Enters the Search
GridSearchCV tries each candidate value in a parameter grid, scores each one, and keeps the best. Scoring is the step that turns each candidate into a comparable number. The diagram follows the data from the parameter grid down to the best_params_ attribute, so you can see exactly where the metric is used.
Notice the position of the scoring step. The metric does not affect fitting, and it does not affect which candidates are generated. It affects only the comparison at the end, and that comparison decides which candidate's settings you keep. This is why two searches over the same grid can return different winners.
Following One Candidate Through the Folds
The scoring step is applied per fold, not once to the whole training set. For a single candidate, the training data is split into folds. Each fold takes a turn as held-out data while the candidate is fitted on the remaining folds, and the chosen metric is computed on the held-out fold. The per-fold numbers are then averaged. That average is the single number grid search compares against the other candidates.
Because the same metric is used on every fold and then averaged, a different metric produces a different average for the same candidate. The folds and the fitted models stay the same. Only the per-fold numbers and their average change.
Reading the Accuracy and AUC Runs
The source uses a deliberately narrow grid that varies only gamma, so the effect of the metric is easy to see. The first search uses the default accuracy. The second uses AUC, which is requested with the string roc_auc. Both searches are fitted on the same training split and evaluated on the same test split.
Two searches, two winners
The same gamma grid is searched twice on the same data. Once with the default accuracy, and once with roc_auc. Which gamma does each search keep, and why do they differ?
- 1Identify the fixed parts
The grid, the classifier, the training split, and the test split are the same in both runs. Only the scoring argument changes.
- 2Read the accuracy run
Under accuracy, the search keeps gamma=0.0001, and its cross-validation accuracy is 0.970.
- 3Read the AUC run
Under AUC, the search keeps gamma=0.01, and its cross-validation AUC is 0.997.
- 4Locate the cause
The fitting and candidate generation did not change. The scoring step at the comparison is the only place the two runs diverge, so it explains the different winners.
- 5Check the held-out data
The source states that the cross-validation accuracy agrees with the test set accuracy in both cases, so the scores are not misleading for this run.
Each search is correct by the metric it was given. Accuracy keeps gamma=0.0001, and AUC keeps gamma=0.01.
Scoring Strings for Classification and Regression
scikit-learn offers many scoring strings. The source highlights a small set that matters most. For classification, accuracy is the default. The string roc_auc asks for the area under the ROC curve, average_precision asks for the area under the precision-recall curve, and the f1 family covers the binary F1 score and its weighted variants. For regression, the commonly used strings are r2 for the R squared score, mean_squared_error for mean squared error, and mean_absolute_error for mean absolute error.
| Scoring string | Task | Metric it asks for | Direction for the search |
|---|---|---|---|
| accuracy | Classification | Accuracy (the default) | Higher is better |
| roc_auc | Classification | Area under the ROC curve | Higher is better |
| average_precision | Classification | Area under the precision-recall curve | Higher is better |
| f1 | Classification | Binary F1 score | Higher is better |
| f1_macro | Classification | F1 averaged across classes, unweighted | Higher is better |
| f1_micro | Classification | F1 from pooled counts across classes | Higher is better |
| f1_weighted | Classification | F1 averaged across classes, weighted by class size | Higher is better |
| r2 | Regression | R squared score | Higher is better |
| neg_mean_squared_error | Regression | Mean squared error, negated | Higher is better after negation |
| neg_mean_absolute_error | Regression | Mean absolute error, negated | Higher is better after negation |
Commonly used scikit-learn scoring strings. The f1 variant descriptions are standard definitions; check your installed version's documentation for exact behavior.
The source also shows how to list every available name by reading the scorer dictionary in the metrics module. That list is long and includes entries such as log_loss, adjusted_rand_score, and several precision and recall variants. The source's import path for that dictionary is an older one, so confirm it against your installed scikit-learn documentation. For everyday work, the names in the table are the ones to learn first.
Changing Only the Scoring Argument in Code
The same argument works in both tools. You can judge one model with cross_val_score and then search over settings with GridSearchCV using the same metric. The example below uses a narrow gamma-only grid in the spirit of the source. The grid values here are illustrative, and the source's own run is the one whose results are reported above.
- scoring="
roc_auc" incross_val_scorereturns one score per fold, using the same metric the search will compare - scoring="accuracy" is the default for classification, so passing it explicitly does not change the results
- Only the scoring argument differs between the two GridSearchCV runs
Choose the scoring string before you run the search, and write it into the code explicitly. An implicit default is easy to forget when you later compare two searches, and an explicit argument makes the comparison number visible in the code.
Mistakes When a Selection Looks Surprising
Common mistake Assuming the search optimizes the same thing as your final evaluation
What it looks likeYou report a model using AUC but select its parameters with the default accuracy, then wonder why the chosen gamma looks wrong for AUC.
Why it failsThe metric enters at the comparison step. If the comparison uses accuracy, the winner is the best by accuracy, whatever you report later.
Do this insteadPass the same scoring string to GridSearchCV that you care about, and to cross_val_score when you judge a single model.
Common mistake Looking for the bug in the fitting code when the winner changes
What it looks likeTwo searches return different best_params_, and you rerun the fit repeatedly to find the cause.
Why it failsFitting and candidate generation do not depend on the metric. The difference lives in the scoring comparison.
Do this insteadCheck the scoring argument first, then compare the candidate scores under that same metric.
Common mistake Treating an error metric as if larger were better
What it looks likeYou pass mean_squared_error to a search and expect the search to prefer the largest error values.
Why it failsA search keeps the highest score, so a loss that should be minimized has to be negated to fit that rule.
Do this insteadUse the negated scoring name, such as neg_mean_squared_error, and confirm it in your installed version's documentation.
Common mistake Assuming that because the cross-validation score is high, the choice is correct for every metric
Predicting Which Metric Changes the Winner
A colleague runs a gamma grid search with the default scoring and keeps gamma=0.0001. They then rerun it with roc_auc on the same data and get gamma=0.01. Before you check anything, predict which step of the workflow could be responsible, then state what you would print to verify your explanation.
Hints
- Look at where the scoring step sits in the pipeline: it affects the comparison, not the fitting.
- Compare the cross-validation scores under the same metric that each search used.
- Check the scoring argument in each GridSearchCV call before anything else.
Key Takeaways
- The scoring argument, passed as a string, sets the metric that
cross_val_scoreand GridSearchCV use to evaluate candidates. - Scoring enters a grid search at the comparison step: fitting and candidate generation do not depend on the metric, so changing it can change best_params_.
- When a selected parameter surprises you, check the scoring argument first, then compare candidate scores using that same metric.
- accuracy is the default for classification, and common alternatives include
roc_auc,average_precision, and the f1 family; common regression strings include r2 and the negated error metrics.