Home » #Technology » Random Search Hyperparameter Tuning in Python SciKit Learn

Random Search Hyperparameter Tuning in Python SciKit Learn

Hyperparameter tuning is a critical step in optimizing machine learning models. Random Search is a powerful alternative to Grid Search that efficiently explores a broad range of hyperparameters in less time. In my 20-year tech career, I’ve been a catalyst for innovation, architecting scalable solutions that lead organizations to extraordinary achievements. My trusted advice inspires businesses to take bold steps and conquer the future of technology. This guide explains Random Search with an example using RandomizedSearchCV in Python to optimize a Random Forest Classifier.

For more detail hyperparamter information: Hyperparameter Tuning for Optimal Model Performance: Finding the Perfect Balance for Machine Learning Models >>

What is Random Search?

Random Search selects hyperparameter values randomly instead of exhaustively testing all combinations. This makes it:

  • Faster than Grid Search: It avoids testing unnecessary configurations.
  • More efficient for large search spaces: It explores a wider range of hyperparameters.
  • Highly effective: Studies show it often finds optimal settings faster than Grid Search.

When to Use Random Search?

  • When the hyperparameter space is large.
  • When you need faster tuning with fewer computations.
  • When deep learning models require tuning.

Example: Random Search for Hyperparameter Tuning in Random Forest

Step 1: Import Libraries & Load Dataset

import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, RandomizedSearchCV
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_iris

# Load Iris dataset
data = load_iris()
X, y = data.data, data.target

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Step 2: Define the Hyperparameter Distribution

from scipy.stats import randint

# Define hyperparameter distributions
param_dist = {
    'n_estimators': randint(50, 300),  # Number of trees
    'max_depth': randint(5, 20),       # Maximum depth of trees
    'criterion': ['gini', 'entropy']   # Splitting criterion
}

Step 3: Perform Random Search

# Initialize the model
rf = RandomForestClassifier(random_state=42)

# Perform Randomized Search with cross-validation
random_search = RandomizedSearchCV(estimator=rf, param_distributions=param_dist,
                                   n_iter=10, cv=5, scoring='accuracy',
                                   n_jobs=-1, random_state=42, verbose=1)

# Fit the model
random_search.fit(X_train, y_train)

Step 4: Get the Best Parameters & Evaluate Performance

# Get the best combination of hyperparameters
best_params = random_search.best_params_
print("Best Hyperparameters:", best_params)

# Train the model with best hyperparameters
best_model = random_search.best_estimator_
y_pred = best_model.predict(X_test)

# Evaluate model performance
accuracy = accuracy_score(y_test, y_pred)
print(f"Best Model Accuracy: {accuracy:.4f}")

Why Choose Random Search Over Grid Search?

  • Faster exploration: Random Search tests fewer combinations but covers a broader range.
  • More efficient for large search spaces: It can quickly identify promising hyperparameter settings.
  • Scalable: Works well with deep learning models and large datasets.

My Tech Advice: Random Search is a powerful and efficient hyperparameter tuning technique. It provides faster and often better results than Grid Search, making it an ideal choice for optimizing machine learning models. For further smarter optimisation, consider Bayesian Optimisation, Hyperband.

#AskDushyant
Note: The example and pseudo code is for illustration only. You must modify and experiment with the concept to meet your specific needs.
#TechConcept #TechAdvice #AI #ML #Python #ModelTuning

Leave a Reply

Your email address will not be published. Required fields are marked *