Hyperparameter Tuning: Grid Search, Random Search, and Bayesian Optimization
Learn how to find the best settings for your ML models using grid search, random search, and Bayesian optimization, with practical guidance on what to tune for each algorithm.
Hyperparameter Tuning: Grid Search, Random Search, and Bayesian Optimization
Hyperparameters are the settings you choose before training a model. Unlike model parameters (learned from data), hyperparameters control how the model learns. Tuning them correctly can dramatically improve performance.
---
Parameters vs Hyperparameters
| Type | Set By | Example |
|---|---|---|
| Parameters | Learned from data during training | Regression coefficients, tree splits |
| Hyperparameters | Set by you before training | Learning rate, number of trees, max depth |
---
Common Hyperparameters by Algorithm
Decision Tree / Random Forest / Gradient Boosting
| Hyperparameter | What It Controls | Typical Range |
|---|---|---|
| maxdepth | How deep the tree grows | 3-20 |
| nestimators | Number of trees (ensemble) | 50-1000 |
| minsamplessplit | Min samples to split a node | 2-20 |
| minsamplesleaf | Min samples in a leaf node | 1-10 |
| maxfeatures | Features considered per split | "sqrt", "log2", 0.5-1.0 |
| learningrate (boosting) | Step size per tree | 0.001-0.3 |
| subsample (boosting) | Fraction of data per tree | 0.5-1.0 |
SVM (Support Vector Machine)
| Hyperparameter | What It Controls | Typical Range |
|---|---|---|
| C | Regularization strength | 0.01-100 |
| kernel | Type of boundary | "linear", "rbf", "poly" |
| gamma (rbf/poly) | Influence of each training point | "scale", "auto", 0.001-10 |
KNN (K-Nearest Neighbors)
| Hyperparameter | What It Controls | Typical Range |
|---|---|---|
| nneighbors | Number of neighbors | 1-50 |
| weights | How neighbors are weighted | "uniform", "distance" |
| metric | Distance measure | "euclidean", "manhattan" |
Neural Networks
| Hyperparameter | What It Controls | Typical Range |
|---|---|---|
| learningrate | Step size for weight updates | 0.0001-0.01 |
| hiddenlayersizes | Neurons per layer | (50,), (100,50), (200,100,50) |
| activation | Activation function | "relu", "tanh" |
| batchsize | Samples per training step | 16-256 |
| epochs | Full passes through data | 10-500 |
XGBoost / LightGBM
| Hyperparameter | What It Controls | Typical Range |
|---|---|---|
| nestimators | Number of boosting rounds | 100-2000 |
| learningrate | Shrinkage per round | 0.01-0.3 |
| maxdepth | Tree depth | 3-12 |
| regalpha | L1 regularization | 0-10 |
| reglambda | L2 regularization | 0-10 |
| colsamplebytree | Feature fraction per tree | 0.3-1.0 |
Tuning Strategies
Grid Search
Try every combination of specified values.
Example: maxdepth = [3, 5, 7], nestimators = [100, 200, 500] → 3 × 3 = 9 combinations evaluated
| Pros | Cons |
|---|---|
| Thorough, guaranteed to find best in grid | Extremely slow with many hyperparameters |
| Easy to understand and implement | Wastes time on unimportant parameters |
| Good for small search spaces | Scales poorly: 5 params × 5 values = 3,125 combos |
Random Search
Try random combinations from a defined range, for a fixed number of iterations.
Example: maxdepth ~ Uniform(3, 20), nestimators ~ Uniform(50, 1000), 50 random trials
| Pros | Cons |
|---|---|
| Much faster than grid search | May miss the optimal combination |
| Scales well to many hyperparameters | Results are non-deterministic |
| Often finds good solutions quickly | No intelligent search direction |
Bayesian Optimization
Uses past results to intelligently choose the next combination to try. Builds a model of the relationship between hyperparameters and performance.
| Pros | Cons |
|---|---|
| Most sample-efficient | More complex to implement |
| Learns from previous evaluations | Overhead per iteration |
| Converges faster to optimal | Can get stuck in local optima |
Early Stopping
Not a search method, but a complement: stop training when performance stops improving.
| Parameter | Meaning |
|---|---|
| patience | Number of rounds without improvement before stopping |
| mindelta | Minimum improvement to count as progress |
---
How to Tune in the Platform
Using Expert Config
- Go to ML → Expert
- Select your algorithm
- Expand the hyperparameter panel
- Set values or ranges for each parameter
- Choose search strategy: Grid, Random, or Auto
- Set the number of trials (for random/Bayesian)
- Click Train — the platform evaluates each combination using cross-validation
Using AutoML
AutoML automatically searches for the best algorithm and hyperparameters. It uses intelligent search under the hood.
- Go to ML → AutoML
- Select dataset and target
- Set time budget or max trials
- AutoML handles tuning automatically
Tuning Workflow
---1. Start with default hyperparameters → baseline score ↓↓
- Identify which hyperparameters matter most
↓
- Run a coarse search (wide range, few trials)
↓
- Narrow the range around the best values
↓
- Run a fine search (tight range, more trials)
↓
- Evaluate the best model with cross-validation
Train final model on all training data with best hyperparameters
Which Hyperparameters Matter Most?
Not all hyperparameters have equal impact. Focus on the most important ones first.
| Algorithm | Most Important | Less Important |
|---|---|---|
| Random Forest | nestimators, maxdepth | minsamplesleaf, maxfeatures |
| XGBoost | learningrate, nestimators, maxdepth | regalpha, reglambda |
| SVM | C, kernel, gamma | — |
| KNN | nneighbors, weights | metric |
| Neural Network | learningrate, architecture | batch_size, activation |
Common Pitfalls
| Pitfall | Problem | Fix |
|---|---|---|
| Tuning on test set | Overly optimistic results | Use nested cross-validation |
| Too large a grid | Takes days to finish | Use random search first |
| Ignoring default values | Defaults are often good starting points | Always compare against defaults |
| Overfitting the validation set | Too many tuning rounds adapt to specific folds | Limit trials, use held-out test set |
| Not using early stopping | Wastes time, may overfit | Enable early stopping for iterative models |
Quick Reference
| Strategy | Speed | Quality | Best For |
|---|---|---|---|
| Grid Search | Slow | Guaranteed best in grid | 1-3 parameters |
| Random Search | Fast | Usually good enough | 3+ parameters |
| Bayesian | Medium | Best | Expensive evaluations |
| AutoML | Varies | Automated | Full pipeline automation |
What's Next?
- [ML Algorithms Guide](ml-algorithms-guide) — All 15+ algorithms explained
- [Cross-Validation Guide](cross-validation-guide) — Evaluate tuned models reliably
- [Expert Config Guide](expert-config-guide) — Advanced model configuration
- [AutoML Guide](automl-guide) — Let the platform tune automatically
Related Articles
ML Algorithms: All 15+ Explained in Plain English
Plain-English explanations of Linear/Ridge/Lasso/Elastic Net, Logistic Regression, Decision Tree, Random Forest, XGBoost, LightGBM, SVM, KNN, Naive Bayes, Neural Network, AdaBoost, Extra Trees—with pros, cons, and a comparison table.
Cross-Validation: Reliable Model Evaluation Techniques
Learn how to evaluate ML models reliably using k-fold, stratified, leave-one-out, and time series cross-validation to avoid overfitting and get honest performance estimates.
Expert Config: Hyperparameters, Cross-Validation, and Class Balancing
Understand hyperparameters, algorithm-specific config forms, cross-validation, feature selection, class balancing (SMOTE/undersampling), and training options.
AutoML: Automatic Model Selection and Training
What AutoML is, when to use it, how it works, step-by-step configuration, reading the leaderboard, and selecting the best model.
Was this helpful?