Skip to main content

Lesson 4 of 9

Randomized Search (RandomizedSearchCV) with Continuous Probability Distributions

RandomizedSearchCV samples a fixed budget of N candidate parameter configurations randomly from specified probability distributions (Uniform, Log-Uniform, Integer). Proven by Bergstra & Bengio, Randomized Search explores high-dimensional spaces far more efficiently than Grid Search because it tests unique values for every parameter on every trial, discovering optimal configurations in 1/10th the compute budget.

25 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Randomized Search with SciPy Continuous Distributions

Randomized Search with SciPy Continuous Distributions
import numpy as np
import pandas as pd
from sklearn.model_selection import RandomizedSearchCV, StratifiedKFold
from sklearn.ensemble import HistGradientBoostingClassifier
from scipy.stats import uniform, loguniform, randint

# Generate complex classification dataset
np.random.seed(42)
X = np.random.randn(1500, 15)
y = (X[:, 0] * 1.5 - X[:, 1] * 2.0 + np.random.randn(1500) > 0).astype(int)

# Define continuous probability distributions for hyperparameters
# loguniform: samples evenly across orders of magnitude (e.g. 0.001 to 0.1)
param_distributions = {
    'learning_rate': loguniform(1e-3, 1e-1), # Explores 0.001, 0.01, 0.1 logarithmically
    'max_leaf_nodes': randint(15, 64),       # Discrete integers between 15 and 63
    'min_samples_leaf': randint(5, 50),
    'l2_regularization': uniform(0.0, 10.0)  # Uniform continuous between 0.0 and 10.0
}

hgb = HistGradientBoostingClassifier(max_iter=80, random_state=42)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

random_search = RandomizedSearchCV(
    estimator=hgb,
    param_distributions=param_distributions,
    n_iter=25, # Fixed compute budget: exactly 25 strategic samples
    scoring='roc_auc',
    cv=cv,
    random_state=42,
    n_jobs=-1
)
random_search.fit(X, y)

print("=== RANDOMIZED SEARCH WITH CONTINUOUS DISTRIBUTIONS ===")
print(f"Total Budgeted Trials: {random_search.n_iter}")
print(f"Best 5-Fold CV ROC-AUC: {random_search.best_score_:.4f}")
print("Optimal Sampled Parameters:")
for param, val in random_search.best_params_.items():
    print(f"  {param:20s}: {val:.5f}" if isinstance(val, float) else f"  {param:20s}: {val}")

Pro Tip: When to Use `scipy.stats.loguniform`

Always use `loguniform(1e-4, 1e-1)` for learning rates and regularization penalties (`alpha`, `C`). A standard uniform distribution `uniform(0.0001, 0.1)` places 90% of its samples between 0.01 and 0.1, severely under-sampling the critical small-magnitude region (0.0001 to 0.01). Log-uniform samples evenly across every order of magnitude.

Diagram
Loading diagram…

MlRandomizedSearchCV Visual Representation

Hands-On Challenge: Budget Optimization Benchmark

Run `GridSearchCV` (64 combinations) vs `RandomizedSearchCV` (16 trials) on the same dataset. Measure the wall-clock execution time and prove that Randomized Search achieves 99%+ of the Grid Search score in 1/4th the runtime.

Quick Quiz & Key Concept Checks

Common Pitfall: Specifying Lists Instead of Distributions in RandomizedSearch

Passing static lists (e.g. `{'learning_rate': [0.01, 0.05, 0.1]}`) restricts RandomizedSearchCV to sampling discrete points with replacement. Provide continuous SciPy distributions (`loguniform`, `uniform`) to explore continuous parameter space.

Topics in This Module

0/9 done