Created
July 19, 2021 10:32
-
-
Save mbednarski/caf1fe413ec16774dba9ce9b97c89ed8 to your computer and use it in GitHub Desktop.
Revisions
-
mbednarski created this gist
Jul 19, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ 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) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ 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)