Skip to content

Instantly share code, notes, and snippets.

@mbednarski
Created July 19, 2021 10:32
Show Gist options
  • Save mbednarski/caf1fe413ec16774dba9ce9b97c89ed8 to your computer and use it in GitHub Desktop.
Save mbednarski/caf1fe413ec16774dba9ce9b97c89ed8 to your computer and use it in GitHub Desktop.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
import optuna
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.20, stratify=y)
def objective(trial: optuna.Trial):
params = {
'max_depth': trial.suggest_int('max_depth', 1, 20),
'criterion': trial.suggest_categorical('crterion', ['gini', 'entropy']),
'min_samples_split': trial.suggest_float('min_samples_split', 0.1, 1.0,log=True)
}
clf = DecisionTreeClassifier(**params)
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test) # acc
return score
study = optuna.create_study(study_name='irysy', storage='sqlite:///irysy.db', direction='maximize', load_if_exists=True)
study.optimize(objective, n_trials=20)
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVR
import optuna
X, y = load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.20, stratify=y)
def objective(trial: optuna.Trial):
type_ = trial.suggest_categorical('type_', ['svr', 'rf'])
if type_ == 'rf':
params = {
'max_depth': trial.suggest_int('max_depth', 1, 20),
'criterion': trial.suggest_categorical('crterion', ['gini', 'entropy']),
'min_samples_split': trial.suggest_float('min_samples_split', 0.1, 1.0,log=True),
'max_features': trial.suggest_categorical('max_features', ["auto", "sqrt", "log2"])
}
clf = DecisionTreeClassifier(**params)
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test) # acc
return score
elif type_ == 'svr':
params = {
'kernel': trial.suggest_categorical('kernel', ['linear', 'poly', 'rbf', 'sigmoid']),
'degree': trial.suggest_int('degree', 1, 3),
'C': trial.suggest_float('C', 0.01, 10, log=True)
}
clf = SVR(**params)
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test) # acc
return score
study = optuna.create_study(study_name='digits2', storage='sqlite:///digits2.db', direction='maximize', load_if_exists=True)
study.optimize(objective, n_trials=200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment