

.. _sphx_glr_auto_examples_cluster:

.. _cluster_examples:

Clustering
----------

Examples concerning the :mod:`sklearn.cluster` module.



.. raw:: html

    <div class="sphx-glr-thumbnails">

.. thumbnail-parent-div-open

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example we compare the various initialization strategies for K-means in terms of runtime and quality of the results.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_kmeans_digits_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_kmeans_digits`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">A demo of K-Means clustering on the handwritten digits data</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Compute the segmentation of a 2D image with Ward hierarchical clustering. The clustering is spatially constrained in order for each segmented region to be in one piece.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_coin_ward_segmentation_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_coin_ward_segmentation`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">A demo of structured Ward hierarchical clustering on an image of coins</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Reference:">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_mean_shift_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_mean_shift`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">A demo of the mean-shift clustering algorithm</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="- a first experiment with fixed &quot;ground truth labels&quot; (and therefore fixed   number of classes) and randomly &quot;predicted labels&quot;; - a second experiment with varying &quot;ground truth labels&quot;, randomly &quot;predicted   labels&quot;. The &quot;predicted labels&quot; have the same number of classes and clusters   as the &quot;ground truth labels&quot;.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_adjusted_for_chance_measures_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_adjusted_for_chance_measures`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Adjustment for chance in clustering performance evaluation</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Demonstrates the effect of different metrics on the hierarchical clustering.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_agglomerative_clustering_metrics_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_agglomerative_clustering_metrics`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Agglomerative clustering with different metrics</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="An example to show the output of the sklearn.cluster.kmeans_plusplus function for generating initial seeds for clustering.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_kmeans_plusplus_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_kmeans_plusplus`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">An example of K-Means++ initialization</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example shows differences between Regular K-Means algorithm and Bisecting K-Means.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_bisect_kmeans_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_bisect_kmeans`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Bisecting K-Means and Regular K-Means Performance Comparison</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example compares the timing of BIRCH (with and without the global clustering step) and MiniBatchKMeans on a synthetic dataset having 25,000 samples and 2 features generated using make_blobs.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_birch_vs_minibatchkmeans_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_birch_vs_minibatchkmeans`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Compare BIRCH and MiniBatchKMeans</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example shows characteristics of different clustering algorithms on datasets that are &quot;interesting&quot; but still in 2D. With the exception of the last dataset, the parameters of each of these dataset-algorithm pairs has been tuned to produce good clustering results. Some algorithms are more sensitive to parameter values than others.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_cluster_comparison_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_cluster_comparison`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Comparing different clustering algorithms on toy datasets</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example shows characteristics of different linkage methods for hierarchical clustering on datasets that are &quot;interesting&quot; but still in 2D.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_linkage_comparison_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_linkage_comparison`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Comparing different hierarchical linkage methods on toy datasets</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="We want to compare the performance of the MiniBatchKMeans and KMeans: the MiniBatchKMeans is faster, but gives slightly different results (see mini_batch_kmeans).">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_mini_batch_kmeans_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_mini_batch_kmeans`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Comparison of the K-Means and MiniBatchKMeans clustering algorithms</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="DBSCAN (Density-Based Spatial Clustering of Applications with Noise) finds core samples in regions of high density and expands clusters from them. This algorithm is good for data which contains clusters of similar density.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_dbscan_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_dbscan`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Demo of DBSCAN clustering algorithm</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this demo we will take a look at cluster.HDBSCAN from the perspective of generalizing the cluster.DBSCAN algorithm. We&#x27;ll compare both algorithms on specific datasets. Finally we&#x27;ll evaluate HDBSCAN&#x27;s sensitivity to certain hyperparameters.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_hdbscan_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_hdbscan`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Demo of HDBSCAN clustering algorithm</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Finds core samples of high density and expands clusters from them. This example uses data that is generated so that the clusters have different densities.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_optics_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_optics`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Demo of OPTICS clustering algorithm</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Reference: Brendan J. Frey and Delbert Dueck, &quot;Clustering by Passing Messages Between Data Points&quot;, Science Feb. 2007">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_affinity_propagation_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_affinity_propagation`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Demo of affinity propagation clustering algorithm</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example is meant to illustrate situations where k-means produces unintuitive and possibly undesirable clusters.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_kmeans_assumptions_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_kmeans_assumptions`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Demonstration of k-means assumptions</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Evaluate the ability of k-means initializations strategies to make the algorithm convergence robust, as measured by the relative standard deviation of the inertia of the clustering (i.e. the sum of squared distances to the nearest cluster center).">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_kmeans_stability_low_dim_dense_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_kmeans_stability_low_dim_dense`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Empirical evaluation of the impact of k-means initialization</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="These images show how similar features are merged together using feature agglomeration.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_digits_agglomeration_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_digits_agglomeration`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Feature agglomeration</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example compares 2 dimensionality reduction strategies:">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_feature_agglomeration_vs_univariate_selection_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_feature_agglomeration_vs_univariate_selection`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Feature agglomeration vs. univariate selection</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates hierarchical clustering with and without connectivity constraints. It shows the effect of imposing a connectivity graph to capture local structure in the data. Without connectivity constraints, the clustering is based purely on distance, while with constraints, the clustering respects local structure.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_ward_structured_vs_unstructured_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_ward_structured_vs_unstructured`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Hierarchical clustering with and without structure</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Clustering can be expensive, especially when our dataset contains millions of datapoints. Many clustering algorithms are not inductive and so cannot be directly applied to new data samples without recomputing the clustering, which may be intractable. Instead, we can use clustering to then learn an inductive model with a classifier, which has several benefits:">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_inductive_clustering_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_inductive_clustering`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Inductive Clustering</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example uses a large dataset of faces to learn a set of 20 x 20 images patches that constitute faces.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_dict_face_patches_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_dict_face_patches`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Online learning of a dictionary of parts of faces</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Plot Hierarchical Clustering Dendrogram">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_agglomerative_dendrogram_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_agglomerative_dendrogram`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Plot Hierarchical Clustering Dendrogram</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example uses spectral_clustering on a graph created from voxel-to-voxel difference on an image to break this image into multiple partly-homogeneous regions.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_coin_segmentation_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_coin_segmentation`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Segmenting the picture of greek coins in regions</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Silhouette analysis can be used to study the separation distance between the resulting clusters. The silhouette plot displays a measure of how close each point in one cluster is to points in the neighboring clusters and thus provides a way to assess parameters like number of clusters visually. This measure has a range of [-1, 1].">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_kmeans_silhouette_analysis_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_kmeans_silhouette_analysis`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Selecting the number of clusters with silhouette analysis on KMeans clustering</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example, an image with connected circles is generated and spectral clustering is used to separate the circles.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_segmentation_toy_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_segmentation_toy`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Spectral clustering for image segmentation</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="An illustration of various linkage option for agglomerative clustering on a 2D embedding of the digits dataset.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_digits_linkage_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_digits_linkage`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Various Agglomerative Clustering on a 2D embedding of digits</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example shows how one can use KBinsDiscretizer to perform vector quantization on a set of toy image, the raccoon face.">

.. only:: html

  .. image:: /auto_examples/cluster/images/thumb/sphx_glr_plot_face_compress_thumb.png
    :alt:

  :doc:`/auto_examples/cluster/plot_face_compress`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Vector Quantization Example</div>
    </div>


.. thumbnail-parent-div-close

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /auto_examples/cluster/plot_kmeans_digits
   /auto_examples/cluster/plot_coin_ward_segmentation
   /auto_examples/cluster/plot_mean_shift
   /auto_examples/cluster/plot_adjusted_for_chance_measures
   /auto_examples/cluster/plot_agglomerative_clustering_metrics
   /auto_examples/cluster/plot_kmeans_plusplus
   /auto_examples/cluster/plot_bisect_kmeans
   /auto_examples/cluster/plot_birch_vs_minibatchkmeans
   /auto_examples/cluster/plot_cluster_comparison
   /auto_examples/cluster/plot_linkage_comparison
   /auto_examples/cluster/plot_mini_batch_kmeans
   /auto_examples/cluster/plot_dbscan
   /auto_examples/cluster/plot_hdbscan
   /auto_examples/cluster/plot_optics
   /auto_examples/cluster/plot_affinity_propagation
   /auto_examples/cluster/plot_kmeans_assumptions
   /auto_examples/cluster/plot_kmeans_stability_low_dim_dense
   /auto_examples/cluster/plot_digits_agglomeration
   /auto_examples/cluster/plot_feature_agglomeration_vs_univariate_selection
   /auto_examples/cluster/plot_ward_structured_vs_unstructured
   /auto_examples/cluster/plot_inductive_clustering
   /auto_examples/cluster/plot_dict_face_patches
   /auto_examples/cluster/plot_agglomerative_dendrogram
   /auto_examples/cluster/plot_coin_segmentation
   /auto_examples/cluster/plot_kmeans_silhouette_analysis
   /auto_examples/cluster/plot_segmentation_toy
   /auto_examples/cluster/plot_digits_linkage
   /auto_examples/cluster/plot_face_compress

