.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_neighborhoods/plot_03_wine.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_04_neighborhoods_plot_03_wine.py: Fuzzy neighborhood distances on the Wine data ============================================= On the 13-dimensional Wine data, MDS of the Euclidean distances (after standardization) shows three overlapping groups. MDS of the fuzzy neighborhood distances pulls the cultivars apart, and an average-linkage dendrogram of the same distances recovers them with few errors, whereas Jarvis--Patrick clustering depends strongly on its parameters. .. GENERATED FROM PYTHON SOURCE LINES 11-40 .. image-sg:: /auto_examples/04_neighborhoods/images/sphx_glr_plot_03_wine_001.png :alt: MDS, Euclidean distances, MDS, fuzzy neighborhood distances, average linkage dendrogram :srcset: /auto_examples/04_neighborhoods/images/sphx_glr_plot_03_wine_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none fuzzy neighborhood + average linkage: 0.9326 Jarvis-Patrick k=10 l=5: 0.3596 Jarvis-Patrick k=10 l=3: 0.3933 Jarvis-Patrick k=8 l=3: 0.3596 Jarvis-Patrick k=8 l=2: 0.3652 | .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from graphclust import FuzzyNeighborhoodClustering, JarvisPatrick, MetricMDS from graphclust.datasets import load_wine from graphclust.plotting import plot_dendrogram, plot_partition from graphclust.validity import clustering_rate X, y = load_wine() X = (X - X.mean(0)) / X.std(0) # [snippet:wine] fnc = FuzzyNeighborhoodClustering(n_clusters=3, k=6, r_max=3, alpha=0.2, linkage="average").fit(X) print("fuzzy neighborhood + average linkage:", round(clustering_rate(y, fnc.labels_), 4)) # [/snippet:wine] for k, l in [(10, 5), (10, 3), (8, 3), (8, 2)]: print(f"Jarvis-Patrick k={k} l={l}:", round(clustering_rate(y, JarvisPatrick(k, l).fit(X).labels_), 4)) D = 1 - fnc.similarity_ np.fill_diagonal(D, 0) fig, axes = plt.subplots(1, 3, figsize=(13, 4)) plot_partition(MetricMDS(random_state=0).fit_transform(X), y, ax=axes[0], title="MDS, Euclidean distances") plot_partition(MetricMDS(dissimilarity="precomputed", random_state=0).fit_transform(D), y, ax=axes[1], title="MDS, fuzzy neighborhood distances") plot_dendrogram(fnc.linkage_matrix_, ax=axes[2], title="average linkage dendrogram") for ax in axes[:2]: ax.set_xticks([]) ax.set_yticks([]) plt.tight_layout() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.203 seconds) .. _sphx_glr_download_auto_examples_04_neighborhoods_plot_03_wine.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/04_neighborhoods/plot_03_wine.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_wine.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_wine.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_wine.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_