Skip to content

Instantly share code, notes, and snippets.

View pkgandhi's full-sized avatar

Pratik Gandhi pkgandhi

  • Boston
View GitHub Profile
@pkgandhi
pkgandhi / slice_plot.py
Created August 17, 2020 00:45
Slice Plot
#Visualize the slice plot
optuna.visualization.plot_slice(study, params=['rf_n_estimators', 'rf_max_depth'])
@pkgandhi
pkgandhi / parallel_coordinate.py
Created August 17, 2020 00:43
Parallel Coordinate
#Visualize the parallel coordinate
optuna.visualization.plot_parallel_coordinate(study, params=['rf_n_estimators', 'rf_max_depth'])
@pkgandhi
pkgandhi / opt_history.py
Created August 17, 2020 00:42
Plot Optimization History
# Visualize the optimization history.
plot_optimization_history(study).show()
@pkgandhi
pkgandhi / parameters.py
Created August 16, 2020 20:27
Defining the parameters
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)
# 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))
@pkgandhi
pkgandhi / attributes.py
Last active August 16, 2020 21:07
Adding attributes in the code
# 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:
@pkgandhi
pkgandhi / optimize.py
Last active August 16, 2020 20:50
Optimization code for multiple runs
# 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)
@pkgandhi
pkgandhi / trials_df.py
Last active August 15, 2020 17:34
trails as data frame
study.trials_dataframe()
@pkgandhi
pkgandhi / store.py
Last active August 13, 2020 11:59
Creating a study and storing
# 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:
@pkgandhi
pkgandhi / study_results.py
Last active August 16, 2020 18:32
Getting the results from study object
# 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: