How to Do Random Search for Hyperparameter Tuning in Python
A mock random search that samples hyperparameter combinations from a grid and ranks them by a dummy score, with a reproducible seed.
Python code
27 linesimport random
# Mock random search over a small hyperparameter grid
param_grid = {
"learning_rate": [0.001, 0.01, 0.1],
"batch_size": [16, 32, 64],
"num_layers": [1, 2, 3]
}
def random_search(grid, n_iter=5, seed=42):
"""Perform random search over a hyperparameter grid."""
random.seed(seed)
keys = list(grid.keys())
results = []
for _ in range(n_iter):
sample = {k: random.choice(grid[k]) for k in keys}
# Mock model performance: lower is better (e.g., validation loss)
score = random.uniform(0.5, 1.0)
results.append((score, sample))
# Return best (lowest score) first
results.sort(key=lambda x: x[0])
return results
if __name__ == "__main__":
best_trials = random_search(param_grid, n_iter=5)
for score, params in best_trials:
print(f"Score: {score:.4f} | Params: {params}")
Output
Score: 0.5782 | Params: {'learning_rate': 0.1, 'batch_size': 16, 'num_layers': 1}
Score: 0.7880 | Params: {'learning_rate': 0.01, 'batch_size': 32, 'num_layers': 3}
Score: 0.8541 | Params: {'learning_rate': 0.01, 'batch_size': 16, 'num_layers': 2}
Score: 0.9177 | Params: {'learning_rate': 0.001, 'batch_size': 64, 'num_layers': 2}
Score: 0.9728 | Params: {'learning_rate': 0.01, 'batch_size': 64, 'num_layers': 1}
How it works
The function random_search uses random.seed(seed) to make the sampling reproducible, which is essential for comparing experiments. For each iteration, it picks one value per hyperparameter using random.choice from the grid lists, then simulates a model score with random.uniform. The results are sorted by score to surface the best combination first. This pattern mirrors how libraries like scikit-learn's RandomizedSearchCV work, but with a simple mock scoring function. The if __name__ == "__main__" guard lets the function be imported without executing the demo loop.
Common mistakes
- Forgetting to seed `random` which makes runs non-reproducible
- Assuming the grid values are continuous; random choice only picks from given options
- Using the same random seed for other random operations later, causing unintended correlation
Variations
- Replace the mock score with a real model evaluation (e.g., cross_val_score) for actual tuning
- Sample continuous ranges with `random.uniform(low, high)` instead of discrete choices
Real-world use cases
- Quickly probing a hyperparameter space when you lack time or compute for full grid search.
- Setting up a reproducible baseline experiment compare against later tuning runs.
- Generating random configurations for a study on sensitivity before investing in Bayesian optimization.
Sponsored
More from ML engineering pipelines
- Bayesian Optimization in Python: A Simplified Mock Implementation medium
- Build a Data Helper Class in Python for ML Pipelines easy
- Build a Mock Random Forest Classifier in Python easy
- Champion Challenger Deployment Mock in Python easy
- Compare Model A vs Model B Metrics in Python easy
- Create a Minimal Great Expectations Suite Mock in Python easy
Keep learning
Related tutorials and quizzes for this topic.