.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_evolution.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_evolution.py: Evolution (logbook) graph ==================================== mloptimizer provides a function to plot the evolution of the fitness function. .. GENERATED FROM PYTHON SOURCE LINES 6-16 .. code-block:: default from sklearn.tree import DecisionTreeClassifier from mloptimizer.application.reporting.plots import plotly_logbook from mloptimizer.domain.evaluation import kfold_stratified_score import plotly import os from sklearn.datasets import load_iris from mloptimizer.interfaces import HyperparameterSpaceBuilder, GeneticSearch from sklearn.model_selection import StratifiedKFold .. GENERATED FROM PYTHON SOURCE LINES 17-19 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 .. GENERATED FROM PYTHON SOURCE LINES 19-21 .. code-block:: default X, y = load_iris(return_X_y=True) .. GENERATED FROM PYTHON SOURCE LINES 22-25 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 .. GENERATED FROM PYTHON SOURCE LINES 25-27 .. code-block:: default hyperparam_space = HyperparameterSpaceBuilder.get_default_space(estimator_class=DecisionTreeClassifier) .. GENERATED FROM PYTHON SOURCE LINES 28-29 The GeneticSearch class is the main wrapper for the optimization of a machine learning model. .. GENERATED FROM PYTHON SOURCE LINES 29-37 .. code-block:: default cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42) opt = GeneticSearch( estimator_class=DecisionTreeClassifier, hyperparam_space=hyperparam_space, **{"generations": 30, "population_size": 100}, # eval_function=kfold_stratified_score, # Deprecated cv=cv ) .. GENERATED FROM PYTHON SOURCE LINES 38-39 To optimizer the classifier we need to call the fit method. .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: default opt.fit(X, y) .. rst-class:: sphx-glr-script-out .. code-block:: none Genetic execution: 0%| | 0/31 [00:00
GeneticSearch(cv=StratifiedKFold(n_splits=5, random_state=42, shuffle=True),
                  estimator_class=<class 'sklearn.tree._classes.DecisionTreeClassifier'>,
                  generations=30,
                  hyperparam_space=HyperparameterSpace(fixed_hyperparams={'criterion': 'gini'}, evolvable_hyperparams={'min_samples_split': Hyperparam('min_samples_split', 2, 50, 'int'), 'min_samples_leaf': Hyperparam('min_samples_leaf', 1, 20, 'int'), 'max_depth': Hyperparam('max_depth', 2, 20, 'int'), 'min_impurity_decrease': Hyperparam('min_impurity_decrease', 0, 150, 'float', 1000), 'ccp_alpha': Hyperparam('ccp_alpha', 0, 300, 'float', 100000)}),
                  population_size=100, seed=175071)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 43-47 We can plot the evolution of the fitness function. The black lines represent the max and min fitness values across all generations. The green, red and blue line are respectively the max, min and avg fitness value for each generation. Each grey point in the graph represents an individual. .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. code-block:: default population_df = opt.populations_ g_logbook = plotly_logbook(opt.logbook_, population_df) plotly.io.show(g_logbook) .. raw:: html :file: images/sphx_glr_plot_evolution_001.html .. GENERATED FROM PYTHON SOURCE LINES 52-53 At the end of the evolution the graph is saved as an html at the path: .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: default print(opt._optimizer_service.optimizer.tracker.graphics_path) print(os.listdir(opt._optimizer_service.optimizer.tracker.graphics_path)) .. rst-class:: sphx-glr-script-out .. code-block:: none ./20250807_114136_DecisionTreeClassifier/graphics ['search_space.html', 'logbook.html'] .. GENERATED FROM PYTHON SOURCE LINES 58-59 The data to generate the graph is available at the path: .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. code-block:: default print(opt._optimizer_service.optimizer.tracker.results_path) print(os.listdir(opt._optimizer_service.optimizer.tracker.results_path)) del opt .. rst-class:: sphx-glr-script-out .. code-block:: none ./20250807_114136_DecisionTreeClassifier/results ['populations.csv', 'logbook.csv'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 26.392 seconds) .. _sphx_glr_download_auto_examples_plot_evolution.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_evolution.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_evolution.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_