.. 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-31 The GeneticSearch class is the main wrapper for the optimization of a machine learning model. .. GENERATED FROM PYTHON SOURCE LINES 31-36 .. code-block:: default opt = GeneticSearch( estimator_class=DecisionTreeClassifier, hyperparam_space=hyperparam_space, **{"generations": 30, "population_size": 100} ) .. GENERATED FROM PYTHON SOURCE LINES 37-39 To optimizer the classifier we need to call the optimize_clf method. The method returns the best classifier with the best hyperparameters found. .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: default opt.fit(X, y) print(opt.best_estimator_) .. rst-class:: sphx-glr-script-out .. code-block:: none WARNING:root:The folder . already exists and it will be used INFO:mloptimizer.log:Initiating genetic optimization... INFO:mloptimizer.log:Algorithm: Optimizer Genetic execution: 0%| | 0/31 [00:00` .. 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 `_