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.

Easy Python 3.9+ Aug 9, 2026 ML engineering pipelines 13 views 0 copies

Python code

27 lines
Python 3.9+
import 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

stdout
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

  1. Replace the mock score with a real model evaluation (e.g., cross_val_score) for actual tuning
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from ML engineering pipelines

Related tutorials and quizzes for this topic.