.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_clustering/plot_02_hierarchical_maps.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 or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_07_clustering_plot_02_hierarchical_maps.py: Hierarchical clustering and distance maps ========================================= Hierarchical clustering of a distance matrix shows the structure of a collection at every level. Sammon mapping (Sammon, 1969) draws the matrix in two dimensions while preserving small distances; the fuzzy Sammon mapping (Feil, Balaskó and Abonyi, 2007) maps the objects relative to the cluster prototypes using the fuzzy memberships, which makes the map of a large collection fast and faithful to the clustering. .. GENERATED FROM PYTHON SOURCE LINES 12-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mvtsa import clustering as cl from mvtsa import pairwise_distances, viz from mvtsa.datasets import make_cbf from mvtsa.evaluation import clustering_scores C, y = make_cbf(n_per_class=20, random_state=1) D = pairwise_distances(C, metric="dtw", window=0.1) .. GENERATED FROM PYTHON SOURCE LINES 25-27 Dendrogram and ordered distance matrix -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-34 .. code-block:: Python hc = cl.HierarchicalClustering( n_clusters=3, linkage="average", metric="precomputed" ).fit(D) print(clustering_scores(y, hc.labels_)) viz.plot_dendrogram(hc.linkage_matrix_, labels=y, truncate=30) viz.plot_distance_matrix(D, hc.labels_) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/07_clustering/images/sphx_glr_plot_02_hierarchical_maps_001.png :alt: plot 02 hierarchical maps :srcset: /auto_examples/07_clustering/images/sphx_glr_plot_02_hierarchical_maps_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/07_clustering/images/sphx_glr_plot_02_hierarchical_maps_002.png :alt: plot 02 hierarchical maps :srcset: /auto_examples/07_clustering/images/sphx_glr_plot_02_hierarchical_maps_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none {'ari': 0.35748971958754366, 'ami': 0.4398821099950448, 'nmi': 0.4589753715390286, 'purity': 0.75, 'accuracy': 0.75} .. GENERATED FROM PYTHON SOURCE LINES 35-37 Sammon and fuzzy Sammon maps ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 37-47 .. code-block:: Python fcm = cl.FuzzyCMedoids(3, metric="precomputed", random_state=0).fit(D) Y_fuzzy, Z_proto = viz.fuzzy_sammon(D, fcm.memberships_, fcm.medoid_indices_) fig, axes = plt.subplots(1, 2, figsize=(9, 4)) viz.plot_embedding(D, y, method="sammon", ax=axes[0]) for k in np.unique(y): axes[1].scatter(*Y_fuzzy[y == k].T, s=22, label=str(k)) axes[1].scatter(*Z_proto.T, marker="*", s=200, color="k", label="prototypes") axes[1].set_title("fuzzy Sammon mapping") axes[1].legend(frameon=False) .. image-sg:: /auto_examples/07_clustering/images/sphx_glr_plot_02_hierarchical_maps_003.png :alt: Sammon mapping (stress 0.223), fuzzy Sammon mapping :srcset: /auto_examples/07_clustering/images/sphx_glr_plot_02_hierarchical_maps_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/sklearn/manifold/_mds.py:735: FutureWarning: The default value of `init` will change from 'random' to 'classical_mds' in 1.10. To suppress this warning, provide some value of `init`. warnings.warn( /opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/sklearn/manifold/_mds.py:752: FutureWarning: The `dissimilarity` parameter is deprecated and will be removed in 1.10. Use `metric` instead. warnings.warn( /opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/sklearn/manifold/_mds.py:735: FutureWarning: The default value of `init` will change from 'random' to 'classical_mds' in 1.10. To suppress this warning, provide some value of `init`. warnings.warn( /opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/sklearn/manifold/_mds.py:752: FutureWarning: The `dissimilarity` parameter is deprecated and will be removed in 1.10. Use `metric` instead. warnings.warn( .. GENERATED FROM PYTHON SOURCE LINES 48-53 Parameter study: the linkage ---------------------------- Cylinder and bell differ only in which side of the plateau rises slowly, so they are hard to separate without labels: the clustering task is much harder than 1-NN classification on the same distances. .. GENERATED FROM PYTHON SOURCE LINES 53-63 .. code-block:: Python for link in ("single", "complete", "average", "ward"): lab = ( cl.HierarchicalClustering(3, linkage=link, metric="precomputed") .fit(D) .labels_ ) print( f"{link:8s} ARI {clustering_scores(y, lab)['ari']:.3f} cluster sizes {np.bincount(lab).tolist()}" ) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none single ARI 0.020 cluster sizes [4, 55, 1] complete ARI 0.080 cluster sizes [47, 5, 8] average ARI 0.357 cluster sizes [11, 16, 33] ward ARI 0.142 cluster sizes [8, 23, 29] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.366 seconds) .. _sphx_glr_download_auto_examples_07_clustering_plot_02_hierarchical_maps.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/mvtsa/main?urlpath=lab/tree/notebooks/auto_examples/07_clustering/plot_02_hierarchical_maps.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_hierarchical_maps.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_hierarchical_maps.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_hierarchical_maps.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_