.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cluster/plot_affinity_propagation.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_cluster_plot_affinity_propagation.py: ================================================= Demo of affinity propagation clustering algorithm ================================================= Reference: Brendan J. Frey and Delbert Dueck, "Clustering by Passing Messages Between Data Points", Science Feb. 2007 .. GENERATED FROM PYTHON SOURCE LINES 11-21 .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause import numpy as np from sklearn import metrics from sklearn.cluster import AffinityPropagation from sklearn.datasets import make_blobs .. GENERATED FROM PYTHON SOURCE LINES 22-24 Generate sample data -------------------- .. GENERATED FROM PYTHON SOURCE LINES 24-29 .. code-block:: Python centers = [[1, 1], [-1, -1], [1, -1]] X, labels_true = make_blobs( n_samples=300, centers=centers, cluster_std=0.5, random_state=0 ) .. GENERATED FROM PYTHON SOURCE LINES 30-32 Compute Affinity Propagation ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-52 .. code-block:: Python af = AffinityPropagation(preference=-50, random_state=0).fit(X) cluster_centers_indices = af.cluster_centers_indices_ labels = af.labels_ n_clusters_ = len(cluster_centers_indices) print("Estimated number of clusters: %d" % n_clusters_) print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels)) print("Completeness: %0.3f" % metrics.completeness_score(labels_true, labels)) print("V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels)) print("Adjusted Rand Index: %0.3f" % metrics.adjusted_rand_score(labels_true, labels)) print( "Adjusted Mutual Information: %0.3f" % metrics.adjusted_mutual_info_score(labels_true, labels) ) print( "Silhouette Coefficient: %0.3f" % metrics.silhouette_score(X, labels, metric="sqeuclidean") ) .. rst-class:: sphx-glr-script-out .. code-block:: none Estimated number of clusters: 3 Homogeneity: 0.872 Completeness: 0.872 V-measure: 0.872 Adjusted Rand Index: 0.912 Adjusted Mutual Information: 0.871 Silhouette Coefficient: 0.753 .. GENERATED FROM PYTHON SOURCE LINES 53-55 Plot result ----------- .. GENERATED FROM PYTHON SOURCE LINES 55-79 .. code-block:: Python import matplotlib.pyplot as plt plt.close("all") plt.figure(1) plt.clf() colors = plt.cycler("color", plt.cm.viridis(np.linspace(0, 1, 4))) for k, col in zip(range(n_clusters_), colors): class_members = labels == k cluster_center = X[cluster_centers_indices[k]] plt.scatter( X[class_members, 0], X[class_members, 1], color=col["color"], marker="." ) plt.scatter( cluster_center[0], cluster_center[1], s=14, color=col["color"], marker="o" ) for x in X[class_members]: plt.plot( [cluster_center[0], x[0]], [cluster_center[1], x[1]], color=col["color"] ) plt.title("Estimated number of clusters: %d" % n_clusters_) plt.show() .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_affinity_propagation_001.png :alt: Estimated number of clusters: 3 :srcset: /auto_examples/cluster/images/sphx_glr_plot_affinity_propagation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.250 seconds) .. _sphx_glr_download_auto_examples_cluster_plot_affinity_propagation.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/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/cluster/plot_affinity_propagation.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_affinity_propagation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_affinity_propagation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_affinity_propagation.zip ` .. include:: plot_affinity_propagation.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_