
Three versions of the same parcel sorter look at one, three, or nine nearby examples before choosing a bin. Which one should you use? You can compare all three on validation data and save a separate set of parcels for the final check. That sequence is the heart of hyperparameter tuning.
A hyperparameter is a setting that controls how a model is fitted, how complex it can become, or how an algorithm makes predictions. A learning rate controls the size of training updates. A tree’s maximum depth limits how far it can split. In a nearest-neighbor classifier, the number of neighbors controls how many examples vote on a prediction.
Parameters and hyperparameters
In a model that predicts delivery time from distance, a coefficient might say how many extra minutes to add per kilometer. The fitting procedure estimates that coefficient from data. It is a model parameter. The learning rate used to update it during training is a hyperparameter.
| What you’re looking at | Example | Where its value comes from |
|---|---|---|
| Model parameter | A fitted linear regression coefficient | The fitting procedure estimates it from training data |
| Hyperparameter | Learning rate or a tree’s maximum depth | A default, a practitioner, or a tuning procedure chooses it |
| Evaluation metric | Accuracy or mean absolute error | You choose what to measure, then calculate it on predictions |
A tuning tool can select settings using data. The distinction is what each step does. Fitting estimates the model’s learned state from training examples. Tuning compares settings for the fitting or prediction procedure using validation results. For a concrete weight update, see Gradient Descent Explained.

Tune the number of neighbors
Imagine a parcel sorter that uses a package’s width and mass to predict whether it belongs in bin A (standard handling) or bin B (special handling). The parcels and labels are fictional, and the bins overlap in width and mass. The example isolates one setting so you can follow the comparison.
With k-nearest neighbors, the closest labeled parcels vote on the bin. Say the three closest are labeled A, B and B, in that order. With k = 1, only the closest parcel votes, so the sorter picks A. With k = 3, B wins two votes to one. With k = 9, more parcels vote, including some that may be less similar. Here, k is a prediction setting even though the algorithm’s fit step largely retains the labeled examples.
What counts as close? In this example, a 5 cm difference in width counts as one step, as does a 250 g difference in mass. The sorter squares the two step differences and adds them to rank nearby parcels. At equal distances, the lower parcel ID comes first.

We start with 32 parcels. Eight are set aside for the final test. The remaining 24 are split into three groups of eight, with both bins represented equally in each group. For each candidate k, two groups supply the reference examples and the third group supplies the validation examples. Rotate the validation group until every development parcel has been evaluated once. Use the same splits and the same accuracy metric for every candidate.

| Neighbors | Fold 1 correct | Fold 2 correct | Fold 3 correct | Mean validation accuracy |
|---|---|---|---|---|
| 1 | 8 of 8 | 7 of 8 | 6 of 8 | 87.5% |
| 3 | 7 of 8 | 6 of 8 | 8 of 8 | 87.5% |
| 9 | 8 of 8 | 6 of 8 | 8 of 8 | 91.7% |
With those distance and tie rules, nine neighbors got 22 of 24 validation parcels right. One and three neighbors each got 21 of 24 right. Because all three folds have the same size, the total proportion correct is also the mean of the fold accuracies. Nine neighbors is the choice for this example, not a universal winner. A different sample or tie rule could change the scores.

Now fix k at 9 and use all 24 development parcels as the reference examples. Run that version on the eight parcels set aside earlier. It gets 8 of 8 right on this small fictional test set. The validation score helped choose the setting. The final test score describes the selected version on those eight separate parcels; it does not show how well the sorter will perform on future parcels.

Keep the final test separate
- Split before tuning. Reserve the final test data. Choose validation splits that fit the problem. If several rows come from the same person or device, keep related rows together. For a forecasting task, respect time order.
- Pick a metric and a short list of settings. For the balanced toy parcels, accuracy is easy to read. In a real problem, choose a measure that reflects the errors you care about and look beyond a single score when the decision calls for it.
- Compare settings on the same development splits. If you scale inputs, fill missing values, or select features, learn those transformations inside each training fold and apply them to that fold’s validation rows.
- Choose, refit and test. Select the settings using validation results, refit on the development data, and evaluate the final configuration on the reserved test data. If you use test feedback to make more choices, reserve fresh data for another independent final check.
| Data branch | What happens |
|---|---|
| 24 development parcels | Compare on three folds, choose k = 9, then refit on all 24. |
| 8 reserved test parcels | Keep apart while choosing k. Evaluate the refitted sorter once. |
Start with a small search
A grid search tries all the combinations you list. Three values of k and two voting rules give six candidates. A random search samples settings from specified choices or ranges, which can be useful when trying every combination is expensive. For either approach, record the candidates, folds, metric and result so someone else can reproduce the decision.

Start with a sensible default and a few alternatives. A large search costs more and can find a setting that happens to suit the validation splits. Treat small differences like the one additional correct validation prediction in this example as uncertain. Test on data that represents how the model will be used.
Continue with Cross-Validation Explained to see how the groups rotate, or study Training vs Testing Data for a closer look at the final test.