.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_clustering/plot_01_partitional.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_01_partitional.py: Partitional clustering: k-means, k-Shape, k-medoids =================================================== Partitional clustering needs a distance and a notion of a cluster centre. :class:`~mvtsa.clustering.TimeSeriesKMeans` averages with the arithmetic mean (Euclidean), DBA (DTW; Petitjean et al., 2011) or the soft-DTW barycenter; k-Shape (Paparrizos and Gravano, 2015) uses the shape-based distance and a centroid computed from the leading eigenvector; k-medoids and fuzzy c-medoids only need a distance matrix, so any elastic measure works. The synthetic control chart data (Alcock and Manolopoulos, 1999) have six classes: normal, cyclic, increasing and decreasing trends, upward and downward shifts. .. GENERATED FROM PYTHON SOURCE LINES 15-26 .. code-block:: Python import matplotlib.pyplot as plt import pandas as pd from mvtsa import clustering as cl from mvtsa import viz from mvtsa.datasets import make_synthetic_control from mvtsa.evaluation import clustering_scores C, y = make_synthetic_control(n_per_class=15, random_state=0) .. GENERATED FROM PYTHON SOURCE LINES 27-29 Five algorithms --------------- .. GENERATED FROM PYTHON SOURCE LINES 29-54 .. code-block:: Python models = { "k-means (Euclidean)": cl.TimeSeriesKMeans( 6, metric="euclidean", random_state=0 ), "k-means (DBA)": cl.TimeSeriesKMeans( 6, metric="dtw", window=0.1, max_iter=10, random_state=0 ), "k-Shape": cl.KShape(6, random_state=0), "k-medoids (DTW)": cl.KMedoids( 6, metric="dtw", metric_params={"window": 0.1} ), "fuzzy c-medoids (DTW)": cl.FuzzyCMedoids( 6, metric="dtw", metric_params={"window": 0.1}, random_state=0 ), } rows = [] for name, m in models.items(): m.fit(C) rows.append({"algorithm": name, **clustering_scores(y, m.labels_)}) print(pd.DataFrame(rows).round(3).to_string(index=False)) dba = models["k-means (DBA)"] viz.plot_cluster_prototypes( C, dba.labels_, centers=dba.cluster_centers_, ncols=3 ) plt.show() .. image-sg:: /auto_examples/07_clustering/images/sphx_glr_plot_01_partitional_001.png :alt: cluster 0 (n=11), cluster 1 (n=24), cluster 2 (n=15), cluster 3 (n=6), cluster 4 (n=13), cluster 5 (n=21) :srcset: /auto_examples/07_clustering/images/sphx_glr_plot_01_partitional_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none algorithm ari ami nmi purity accuracy k-means (Euclidean) 0.570 0.704 0.731 0.767 0.767 k-means (DBA) 0.594 0.725 0.751 0.744 0.678 k-Shape 0.527 0.613 0.649 0.744 0.700 k-medoids (DTW) 0.759 0.817 0.834 0.878 0.878 fuzzy c-medoids (DTW) 0.759 0.817 0.834 0.878 0.878 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.320 seconds) .. _sphx_glr_download_auto_examples_07_clustering_plot_01_partitional.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_01_partitional.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_partitional.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_partitional.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_partitional.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_