Search space graph

Search space graph#

mloptimizer provides a function to plot the search space of the optimization.

from mloptimizer.core import Optimizer
from mloptimizer.hyperparams import HyperparameterSpace
from sklearn.tree import DecisionTreeClassifier
from mloptimizer.aux.plots import plotly_search_space
import plotly
import os
from sklearn.datasets import load_iris

Load the iris dataset to obtain a vector of features X and a vector of labels y. Another dataset or a custom one can be used

X, y = load_iris(return_X_y=True)

Define the HyperparameterSpace, you can use the default hyperparameters for the machine learning model that you want to optimize. In this case we use the default hyperparameters for a DecisionTreeClassifier. Another dataset or a custom one can be used

We use the default TreeOptimizer class to optimize a decision tree classifier.

opt = Optimizer(estimator_class=DecisionTreeClassifier, features=X, labels=y,
                hyperparam_space=hyperparam_space, folder="Search_space_example")

To optimizer the classifier we need to call the optimize_clf method. The first argument is the number of generations and the second is the number of individuals in each generation.

clf = opt.optimize_clf(10, 10)
Genetic execution:   0%|          | 0/11 [00:00<?, ?it/s]/home/docs/checkouts/readthedocs.org/user_builds/mloptimizer/envs/dev/lib/python3.10/site-packages/deap/creator.py:185: RuntimeWarning:

A class named 'FitnessMax' has already been created and it will be overwritten. Consider deleting previous creation of that class or rename it.

/home/docs/checkouts/readthedocs.org/user_builds/mloptimizer/envs/dev/lib/python3.10/site-packages/deap/creator.py:185: RuntimeWarning:

A class named 'Individual' has already been created and it will be overwritten. Consider deleting previous creation of that class or rename it.


Genetic execution:  45%|████▌     | 5/11 [00:00<00:00, 48.42it/s]
Genetic execution:  91%|█████████ | 10/11 [00:00<00:00, 48.05it/s]
Genetic execution: 100%|██████████| 11/11 [00:00<00:00, 44.04it/s]

Following we can generate the plot of the search space

population_df = opt.runs[-1].population_2_df()
param_names = list(opt.hyperparam_space.evolvable_hyperparams.keys())
param_names.append("fitness")
df = population_df[param_names]
g_search_space = plotly_search_space(df, param_names)
plotly.io.show(g_search_space)

At the end of the evolution the graph is saved as an html at the path:

print(opt.tracker.graphics_path)
print(os.listdir(opt.tracker.graphics_path))
Search_space_example/20240406_161633_Tracker/graphics
['logbook.html', 'search_space.html']

The data to generate the graph is available at the path:

print(opt.tracker.results_path)
print(os.listdir(opt.tracker.results_path))

del opt
Search_space_example/20240406_161633_Tracker/results
['populations.csv', 'logbook.csv']

Total running time of the script: (0 minutes 1.896 seconds)

Gallery generated by Sphinx-Gallery