Hyperparameter Tuning: 5 Essential Rules That Work

⚡ TL;DR: Hyperparameters are the settings you choose before training. Tune them with random search rather than grid search — for the same compute, random search explores far more distinct values and usually finds a better model.

A model has two kinds of numbers. Parameters are learned during training. Hyperparameters are the ones you set beforehand — how deep the tree may go, how strong the regularisation is, how many neighbours vote. Hyperparameter tuning is finding good values for them.

What you are actually tuning

ModelThe hyperparameters that matter most
Decision treemax_depth, min_samples_leaf
Random forestn_estimators, max_depth, max_features
Gradient boostinglearning_rate, n_estimators, max_depth
k-NNn_neighbors, the distance metric
Logistic regressionC (inverse regularisation), the penalty type

Note that most of these control model complexity — which means hyperparameter tuning is, in practice, mostly about finding the right point between overfitting and underfitting.

Grid search versus random search

Hyperparameter tuning compared: grid search sampling 5 values per axis versus random search sampling 25 distinct values
Both plots show 25 training runs. Grid search tries 5 distinct learning rates; random search tries 25.

Grid search tries every combination in a grid you specify. Thorough, reproducible, and it explodes: 5 values across 4 hyperparameters is 625 combinations, and with 5-fold cross-validation that is 3,125 model fits.

Random search samples combinations at random from ranges you specify, for a fixed budget of runs.

✅ Why random search usually wins: in most problems only one or two hyperparameters really matter — you just do not know which in advance. Grid search spends its 25 runs on only 5 distinct values of each. Random search spends the same 25 runs on 25 distinct values of each. If learning rate is the one that matters, random search has tried five times as many settings of it.

Use grid search when you have two or three hyperparameters and a small set of sensible values. Use random search otherwise — it is the better default.

Search ranges that make sense

Some hyperparameters should be sampled on a log scale, not a linear one. The interesting difference between learning rates of 0.001 and 0.01 is far larger than between 0.10 and 0.11, so sampling uniformly from 0 to 1 wastes almost every run.

🧪 Sensible starting ranges

Log scale: learning rate 0.001 → 0.3 · regularisation C or alpha 0.001 → 100
Linear scale: max_depth 3 → 20 · min_samples_leaf 1 → 50 · n_neighbors 1 → 30

And a rule worth remembering: if your best value sits at the edge of your range, the range was too narrow. Extend it and search again.

The mistake that makes tuning meaningless

⚠️ Never tune against your test set

Try 200 configurations, keep whichever scores best on the test set, and report that number — and you have overfit to the test set through your own choices. The score is optimistic and you have no clean estimate left.

Correct approach: hold out a test set at the very start and do not touch it. Tune using cross-validation on the remaining data. Evaluate the single winning configuration on the test set once, at the end.

The preprocessing rule applies here too — scaling and imputation must sit inside the pipeline being tuned, or every fold leaks.

Beyond random search

Bayesian optimisation uses the results so far to decide what to try next, concentrating on promising regions. Optuna is the usual tool. It is meaningfully more efficient when each training run is expensive; for a model that fits in seconds, random search with more runs is simpler and just as good.

Successive halving starts many configurations on a small slice of the data, discards the worst, and gives survivors more resources. Available in scikit-learn as HalvingRandomSearchCV.

How much does tuning actually buy you?

Less than beginners expect. Typical gains are one to three percentage points. Better features or more data will usually beat any amount of tuning, and a well-chosen model with default settings is often within a couple of points of a heavily tuned one.

✅ A sensible order of work: get a baseline with default settings first. Then improve the data and the features. Tune the hyperparameters last, when you have run out of better ideas — not first, because it feels productive.

🔑 Key Takeaways

  • Hyperparameters are set before training; parameters are learned during it.
  • Random search beats grid search for the same budget, because it tries more distinct values of each setting.
  • Sample learning rates and regularisation strengths on a log scale.
  • A best value at the edge of your range means the range was too narrow.
  • Tune with cross-validation; touch the test set once, at the very end.

Further reading

scikit-learn’s tuning guide documents GridSearchCV, RandomizedSearchCV and the halving variants, and Optuna is the standard tool if you move on to Bayesian optimisation.

Where to go next

Scroll to Top