.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/trees/plot_kauri_example.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_trees_plot_kauri_example.py: ================================================================== Building an unsupervised tree with kernel-kmeans objective: KAURI ================================================================== We show here how to obtain two different decision trees for clustering using two different kernels to accompanny the KAURI method. The KAURI model builds decision trees using gain metrics derived from the squared MMD-GEMINI which are equivalent to KMeans optimisation. .. GENERATED FROM PYTHON SOURCE LINES 12-15 .. code-block:: Python from sklearn import datasets, metrics from gemclus.tree import Kauri, print_kauri_tree .. GENERATED FROM PYTHON SOURCE LINES 16-18 Load the dataset -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-25 .. code-block:: Python iris = datasets.load_iris() X = iris["data"] y = iris["target"] .. GENERATED FROM PYTHON SOURCE LINES 26-28 Create a first tree using a linear kernel -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-37 .. code-block:: Python # Notice that we limit the depth of the tree for simplicity linear_model = Kauri(max_clusters=3, kernel="linear", max_depth=3) y_pred_linear = linear_model.fit_predict(X) print("Score of model is: ", linear_model.score(X)) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/circleci/.local/lib/python3.10/site-packages/sklearn/base.py:474: FutureWarning: `BaseEstimator._validate_data` is deprecated in 1.6 and will be removed in 1.7. Use `sklearn.utils.validation.validate_data` instead. This function becomes public and is part of the scikit-learn developer API. warnings.warn( Score of model is: 9459.167022308022 .. GENERATED FROM PYTHON SOURCE LINES 38-40 Create a second tree using an additive chi2 kernel -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 42-47 .. code-block:: Python additive_chi2_model = Kauri(max_clusters=3, kernel="additive_chi2", max_depth=3) y_pred_additive_chi2 = additive_chi2_model.fit_predict(X) print("Score of model is: ", additive_chi2_model.score(X)) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/circleci/.local/lib/python3.10/site-packages/sklearn/base.py:474: FutureWarning: `BaseEstimator._validate_data` is deprecated in 1.6 and will be removed in 1.7. Use `sklearn.utils.validation.validate_data` instead. This function becomes public and is part of the scikit-learn developer API. warnings.warn( Score of model is: -22.43532371061057 .. GENERATED FROM PYTHON SOURCE LINES 48-50 Evaluate the performances of the model -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. code-block:: Python print("ARI of linear kernel: ", metrics.adjusted_rand_score(y,y_pred_linear)) print("ARI of additive chi2 kernel: ", metrics.adjusted_rand_score(y,y_pred_additive_chi2)) .. rst-class:: sphx-glr-script-out .. code-block:: none ARI of linear kernel: 0.7172759168337549 ARI of additive chi2 kernel: 0.8680377279943841 .. GENERATED FROM PYTHON SOURCE LINES 57-59 Visualise the tree structure -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python print("Structure of the additive chi2 model") print_kauri_tree(additive_chi2_model, iris["feature_names"]) .. rst-class:: sphx-glr-script-out .. code-block:: none Structure of the additive chi2 model Node 0 |=petal width (cm) <= 0.6 | Node 1 | Cluster: 0 |=petal width (cm) > 0.6 | Node 2 | |=petal length (cm) <= 4.7 | | Node 3 | | Cluster: 2 | |=petal length (cm) > 4.7 | | Node 4 | | |=petal width (cm) <= 1.5 | | | Node 5 | | | Cluster: 2 | | |=petal width (cm) > 1.5 | | | Node 6 | | | Cluster: 1 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.017 seconds) .. _sphx_glr_download_auto_examples_trees_plot_kauri_example.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kauri_example.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_kauri_example.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_kauri_example.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_