Optuna Integration

4.8.0 · active · verified Sun Apr 12

Optuna Integration is a Python package that provides extended functionalities for Optuna, an automatic hyperparameter optimization software framework, in combination with various third-party machine learning libraries such as PyTorch, scikit-learn, and TensorFlow. It allows users to seamlessly integrate Optuna's powerful optimization capabilities and pruning mechanisms into their existing ML workflows. The library is currently at version 4.8.0, actively maintained, and follows Optuna's release cadence, typically with multiple releases per major Optuna version.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `OptunaSearchCV` from `optuna_integration.sklearn` to perform hyperparameter optimization for a scikit-learn `SVC` estimator. It defines a search space for the 'C' and 'kernel' parameters and runs a specified number of trials. The example also implicitly shows how an objective function for Optuna works for direct `study.optimize` usage (though `OptunaSearchCV` abstracts this for scikit-learn models).

import optuna
from optuna_integration.sklearn import OptunaSearchCV
from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

def objective_svc(trial):
    svc_c = trial.suggest_float('svc_c', 1e-10, 1e10, log=True)
    svc_gamma = trial.suggest_float('svc_gamma', 1e-10, 1e10, log=True)
    classifier_obj = SVC(C=svc_c, gamma=svc_gamma)
    X, y = load_iris(return_X_y=True)
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
    classifier_obj.fit(X_train, y_train)
    return classifier_obj.score(X_test, y_test)

# Using OptunaSearchCV for a scikit-learn estimator
# This automatically creates an Optuna study and optimizes the hyperparameters
optuna_search = OptunaSearchCV(
    estimator=SVC(gamma='auto', random_state=0),
    param_distributions={
        'C': optuna.distributions.FloatDistribution(1e-10, 1e10, log=True),
        'kernel': ['linear', 'rbf']
    },
    n_trials=10,
    random_state=0,
    cv=3
)
X, y = load_iris(return_X_y=True)
optuna_search.fit(X, y)

print(f"Best parameters found by OptunaSearchCV: {optuna_search.best_params_}")
print(f"Best score found by OptunaSearchCV: {optuna_search.best_score_}")

view raw JSON →