.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_quickstart.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_quickstart.py: Quickstart example ==================================== Quick example of use of the library to optimize a decision tree classifier. Firstly, we import the necessary libraries to get data and plot the results. .. GENERATED FROM PYTHON SOURCE LINES 7-12 .. code-block:: default from sklearn.tree import DecisionTreeClassifier from sklearn.datasets import load_iris from mloptimizer.interfaces import HyperparameterSpaceBuilder, GeneticSearch .. GENERATED FROM PYTHON SOURCE LINES 13-15 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 15-17 .. code-block:: default X, y = load_iris(return_X_y=True) .. GENERATED FROM PYTHON SOURCE LINES 18-19 Split the dataset into training and test sets .. GENERATED FROM PYTHON SOURCE LINES 19-23 .. code-block:: default from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) .. GENERATED FROM PYTHON SOURCE LINES 24-27 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 27-29 .. code-block:: default hyperparam_space = HyperparameterSpaceBuilder.get_default_space(estimator_class=DecisionTreeClassifier) .. GENERATED FROM PYTHON SOURCE LINES 30-36 The GeneticSearch class is the main wrapper for the optimization of a machine learning model. We configure the genetic algorithm parameters: - generations: Number of evolutionary iterations - population_size: Number of hyperparameter configurations per generation Note: These values are reduced for faster documentation builds. For production use, consider generations=30-50 and population_size=50-100. .. GENERATED FROM PYTHON SOURCE LINES 36-50 .. code-block:: default genetic_params = { 'generations': 10, 'population_size': 20, 'seed': 0 } opt = GeneticSearch( estimator_class=DecisionTreeClassifier, hyperparam_space=hyperparam_space, cv=5, scoring='accuracy', **genetic_params ) .. GENERATED FROM PYTHON SOURCE LINES 51-53 To optimize the classifier we need to call the fit method. The method finds the best hyperparameters and stores them in ``best_estimator_``. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: default opt.fit(X, y) print(opt.best_estimator_) .. rst-class:: sphx-glr-script-out .. code-block:: none DecisionTreeClassifier(max_depth=20, random_state=424155) .. GENERATED FROM PYTHON SOURCE LINES 58-60 Train the classifier with the best hyperparameters found Show the classification report and the confusion matrix .. GENERATED FROM PYTHON SOURCE LINES 60-75 .. code-block:: default from sklearn.metrics import classification_report, confusion_matrix, \ ConfusionMatrixDisplay import matplotlib.pyplot as plt y_pred = opt.predict(X_test) cm = confusion_matrix(y_test, y_pred) print(classification_report(y_test, y_pred)) disp = ConfusionMatrixDisplay.from_predictions( y_test, y_pred, display_labels=opt.best_estimator_.classes_, cmap=plt.cm.Blues ) disp.plot() plt.show() del opt .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_quickstart_001.png :alt: plot quickstart :srcset: /auto_examples/images/sphx_glr_plot_quickstart_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_quickstart_002.png :alt: plot quickstart :srcset: /auto_examples/images/sphx_glr_plot_quickstart_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none precision recall f1-score support 0 1.00 1.00 1.00 12 1 1.00 1.00 1.00 10 2 1.00 1.00 1.00 8 accuracy 1.00 30 macro avg 1.00 1.00 1.00 30 weighted avg 1.00 1.00 1.00 30 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.792 seconds) .. _sphx_glr_download_auto_examples_plot_quickstart.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_quickstart.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_quickstart.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_