Skip to content

Commit

Permalink
Add OptunaSearchCV with terminator
Browse files Browse the repository at this point in the history
  • Loading branch information
Alnusjaponica committed Nov 9, 2023
1 parent cc0bc79 commit 6194ac9
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions terminator/terminator_search_cv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Optuna example that optimizes a classifier configuration using OptunaSearchCV.
This example is the same as `sklearn/sklearn_optuna_search_cv_simple.py` except
that you leave termination of the study up to the terminator callback.
"""

import optuna
from optuna.terminator import TerminatorCallback

from sklearn.datasets import load_iris
from sklearn.svm import SVC


if __name__ == "__main__":
clf = SVC(gamma="auto")

param_distributions = {
"C": optuna.distributions.FloatDistribution(1e-10, 1e10, log=True),
"degree": optuna.distributions.IntDistribution(1, 5),
}
terminator = TerminatorCallback()

optuna_search = optuna.integration.OptunaSearchCV(
clf, param_distributions, n_trials=100, timeout=600, verbose=2, callbacks=[terminator]
)

X, y = load_iris(return_X_y=True)
optuna_search.fit(X, y)

print("Best trial:")
trial = optuna_search.study_.best_trial

print(" Value: ", trial.value)
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))

0 comments on commit 6194ac9

Please sign in to comment.