This file contains 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 characters
#Visualize the slice plot | |
optuna.visualization.plot_slice(study, params=['rf_n_estimators', 'rf_max_depth']) |
This file contains 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 characters
#Visualize the parallel coordinate | |
optuna.visualization.plot_parallel_coordinate(study, params=['rf_n_estimators', 'rf_max_depth']) |
This file contains 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 characters
# Visualize the optimization history. | |
plot_optimization_history(study).show() |
This file contains 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 characters
def objective(trial): | |
# Categorical parameter | |
optimizer = trial.suggest_categorical('rf_criterion', ['gini', 'entropy']) | |
# Int parameter | |
num_layers = trial.suggest_int("rf_n_estimators", 10, 1000) | |
# Uniform parameter | |
dropout_rate = trial.suggest_uniform('rf_min_weight_fraction_leaf', 0.0, 1.0) |
This file contains 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 characters
# Importing the Packages: | |
import optuna | |
import pandas as pd | |
from sklearn import linear_model | |
from sklearn import datasets | |
from sklearn import model_selection | |
#Grabbing a sklearn Classification dataset: | |
X,y = datasets.load_breast_cancer(return_X_y=True, as_frame=True) | |
classes = list(set(y)) |
This file contains 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 characters
# Adding Attributes to Trial | |
score = model_selection.cross_val_score(classifier_obj, X, y, n_jobs=-1, cv=3) | |
accuracy = score.mean() | |
trial.set_user_attr('accuracy', accuracy) | |
# Adding Attributes to Study | |
study.set_user_attr('contributors', ['Pratik']) | |
study.set_user_attr('dataset', 'diabetes') | |
# Printing Attributes: |
This file contains 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 characters
# Importing the Packages: | |
import optuna | |
import joblib | |
import pandas as pd | |
from sklearn import linear_model | |
from sklearn import datasets | |
from sklearn import model_selection | |
X,y = datasets.load_diabetes(return_X_y=True, as_frame=True) |
This file contains 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 characters
study.trials_dataframe() |
This file contains 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 characters
# Import the package: | |
import joblib | |
# Create a study name: | |
study_name = 'experiment-C' | |
# Store in DB: | |
study = optuna.create_study(study_name=study_name, storage='sqlite:///tmp/experiments.db', load_if_exists=True) | |
# Store and load using joblib: |
This file contains 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 characters
# Getting the best trial: | |
print(f"The best trial is : \n{study.best_trial}") | |
# >> Output: | |
#The best trial is : | |
#FrozenTrial(number=18, value=0.9631114824097281, datetime_start=datetime.datetime(2020, 8, 16, 14, 24, 37, 407344), datetime_complete=datetime.datetime(2020, 8, 16, 14, 24, 37, 675114), params={'classifier': 'RandomForest', 'rf_n_estimators': 153, 'rf_max_depth': 21}, | |
#distributions={'classifier': CategoricalDistribution(choices=('LogReg', 'RandomForest')), 'rf_n_estimators': IntUniformDistribution(high=1000, low=10, step=1), 'rf_max_depth': IntLogUniformDistribution(high=32, low=2, step=1)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=18, state=TrialState.COMPLETE) | |
# Getting the best score: | |
print(f"The best value is : \n{study.best_value}") | |
# >> Output: |
NewerOlder