Advanced Hyperparameter Tuning

#advance #hyperparameter #learning #machine #ml #model #notes #optuna #sklearn #tensorflow #tuning

Optuna is a hyperparameter optimization framework that automates the search for the optimal hyperparameters of machine learning models. It uses an efficient and adaptable approach based on the idea of Bayesian optimization.

import optuna
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris

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

# Define objective function
def objective(trial):
    # Define hyperparameters to be tuned
    n_estimators = trial.suggest_int('n_estimators', 50, 200)
    max_depth = trial.suggest_categorical('max_depth', [None, 5, 10])
    min_samples_split = trial.suggest_int('min_samples_split', 2, 10)
    min_samples_leaf = trial.suggest_int('min_samples_leaf', 1, 5)

    # Create Random Forest Classifier with hyperparameters
    rf = RandomForestClassifier(
        n_estimators=n_estimators,
        max_depth=max_depth,
        min_samples_split=min_samples_split,
        min_samples_leaf=min_samples_leaf
    )

    # Evaluate model using cross-validation
    score = cross_val_score(rf, X, y, cv=5, n_jobs=-1)
    return score.mean()  # Return the mean cross-validation score

# Create Optuna study object
study = optuna.create_study(direction='maximize')

# Optimize objective function
study.optimize(objective, n_trials=100)

# Best parameters and best score
print("Best Parameters:", study.best_params)
print("Best Score:", study.best_value)