Hyperparameter Tuning in Python — Core Concepts

What Are Hyperparameters?

Machine learning models have two kinds of settings:

  • Parameters — learned automatically from data during training (weights, coefficients).
  • Hyperparameters — set by you before training begins (learning rate, number of trees, regularization strength).

Hyperparameters control how the model learns. Choosing them well can mean the difference between a mediocre model and a great one.

Why Tuning Matters

Default hyperparameters work reasonably well in many cases, but they are generic. Your specific dataset may benefit from different settings. Studies on OpenML benchmarks have shown that tuning can improve accuracy by 3-15 percentage points depending on the model and dataset.

The Main Approaches

Grid search tries every combination of values you specify. If you want to test 3 learning rates and 4 tree depths, it trains 12 models.

  • Pros: Exhaustive, guaranteed to find the best combo in the grid.
  • Cons: Exponentially expensive. Adding one more parameter with 5 values multiplies the cost by 5.
  • Best for: Small search spaces with 2-3 parameters.

Instead of trying every combination, random search samples parameter values randomly from specified ranges. A 2012 paper by Bergstra and Bengio showed that random search finds good settings in far fewer iterations than grid search when some parameters matter more than others.

  • Pros: Explores a wider range, faster for large search spaces.
  • Cons: No guarantee of finding the optimal point.
  • Best for: Medium search spaces, initial exploration.

Bayesian Optimization

Bayesian methods build a probabilistic model of the score landscape and use it to choose the next set of hyperparameters to try. Each trial gives information that makes the next trial smarter.

  • Pros: Finds good settings in fewer trials than random or grid.
  • Cons: More complex to set up, overhead per trial.
  • Best for: Expensive models where each trial takes minutes or hours.

Successive Halving and Hyperband

These methods run many configurations for a few iterations, then discard the worst half and give more resources to the survivors. This repeats until one configuration remains.

  • Pros: Very efficient for models with an iterative training process (neural networks, boosted trees).
  • Cons: Assumes early performance predicts final performance, which is not always true.

How to Choose

ScenarioRecommended Method
2-3 parameters, small datasetGrid search
5+ parameters, moderate dataRandom search
Expensive model, limited budgetBayesian optimization
Many configs, iterative trainingHyperband

Common Misconception

“Tuning is optional if you use a powerful model.” Even gradient boosting machines and neural networks have hyperparameters that dramatically affect performance. XGBoost alone has over 15 tunable parameters. Using defaults is a reasonable starting point, but leaving them untuned is leaving accuracy on the table.

Practical Tips

  • Always tune with cross-validation, not a single train/test split.
  • Start broad (random search) to identify important ranges, then zoom in (grid or Bayesian).
  • Track every experiment: parameters, scores, and training time. Tools like MLflow make this easy.
  • Set a time budget. Diminishing returns kick in quickly — the first 50 trials often find 90 percent of the improvement.

One thing to remember: Hyperparameter tuning is not magic — it is a systematic search. Start simple, measure everything, and stop when the gains no longer justify the compute cost.

pythonhyperparameter-tuningmachine-learningoptimization

See Also