.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_trees/plot_03_curves_initialization.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_03_trees_plot_03_curves_initialization.py: Volume-based splitting as initialization of Gaussian mixtures ============================================================= Curved clusters cannot be described by one Gaussian each, but by several. Run with a generous maximal number of clusters, the hybrid algorithm first separates the curves (Zahn's criteria) and then keeps splitting the most voluminous pieces, so that every piece is roughly ellipsoidal and contains samples of one curve only. Gath--Geva started from this partition converges to a clean mixture; started from random memberships it mixes the curves. A dendrogram of the cluster centers shows which pieces belong together. .. GENERATED FROM PYTHON SOURCE LINES 13-44 .. image-sg:: /auto_examples/03_trees/images/sphx_glr_plot_03_curves_initialization_001.png :alt: hybrid MST-GG, 10 clusters, Gath--Geva from a random start, single linkage of the cluster centers :srcset: /auto_examples/03_trees/images/sphx_glr_plot_03_curves_initialization_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none criteria: ['global', 'global', 'global', 'volume', 'local', 'global', 'global', 'local', 'local'] purity of the pieces: hybrid initialization 0.984, random initialization 0.870 | .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.cluster.hierarchy import linkage from graphclust import GathGeva, HybridMSTGG from graphclust.datasets import make_curves from graphclust.plotting import plot_dendrogram, plot_partition X, y = make_curves() # [snippet:curves] hyb = HybridMSTGG(max_clusters=10, lambda_=2.5).fit(X) Xs = (X - X.min(0)) / np.ptp(X, 0) rnd = GathGeva(10, random_state=0).fit(Xs) # [/snippet:curves] def purity(lab): return np.mean([np.bincount(y[lab == c]).max() / (lab == c).sum() for c in np.unique(lab)]) print("criteria:", [s["criterion"] for s in hyb.split_history_]) print(f"purity of the pieces: hybrid initialization {purity(hyb.labels_):.3f}, random initialization {purity(rnd.labels_):.3f}") fig, axes = plt.subplots(1, 3, figsize=(13, 4)) plot_partition(Xs, hyb.labels_, ax=axes[0], markers=False, title="hybrid MST-GG, 10 clusters") plot_partition(Xs, rnd.labels_, ax=axes[1], markers=False, title="Gath--Geva from a random start") for ax in axes[:2]: ax.set_xticks([]) ax.set_yticks([]) Z = linkage(hyb.cluster_centers_, "single") plot_dendrogram(Z, ax=axes[2], title="single linkage of the cluster centers") plt.tight_layout() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.154 seconds) .. _sphx_glr_download_auto_examples_03_trees_plot_03_curves_initialization.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/graphclust/main?urlpath=lab/tree/notebooks/auto_examples/03_trees/plot_03_curves_initialization.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_curves_initialization.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_curves_initialization.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_curves_initialization.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_