
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/cluster/plot_cluster_iris.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_cluster_plot_cluster_iris.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_cluster_plot_cluster_iris.py:


=========================================================
K-means Clustering
=========================================================

The plots display firstly what a K-means algorithm would yield
using three clusters. It is then shown what the effect of a bad
initialization is on the classification process:
By setting n_init to only 1 (default is 10), the amount of
times that the algorithm will be run with different centroid
seeds is reduced.
The next plot displays what using eight clusters would deliver
and finally the ground truth.

.. GENERATED FROM PYTHON SOURCE LINES 17-93



.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_cluster_iris_001.png
         :alt: 8 clusters
         :srcset: /auto_examples/cluster/images/sphx_glr_plot_cluster_iris_001.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_cluster_iris_002.png
         :alt: 3 clusters
         :srcset: /auto_examples/cluster/images/sphx_glr_plot_cluster_iris_002.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_cluster_iris_003.png
         :alt: 3 clusters, bad initialization
         :srcset: /auto_examples/cluster/images/sphx_glr_plot_cluster_iris_003.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_cluster_iris_004.png
         :alt: Ground Truth
         :srcset: /auto_examples/cluster/images/sphx_glr_plot_cluster_iris_004.png
         :class: sphx-glr-multi-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:55: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
      ax.w_xaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:56: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
      ax.w_yaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:57: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
      ax.w_zaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:62: MatplotlibDeprecationWarning: The dist attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.
      ax.dist = 12
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:55: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
      ax.w_xaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:56: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
      ax.w_yaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:57: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
      ax.w_zaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:62: MatplotlibDeprecationWarning: The dist attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.
      ax.dist = 12
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:55: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
      ax.w_xaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:56: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
      ax.w_yaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:57: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
      ax.w_zaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:62: MatplotlibDeprecationWarning: The dist attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.
      ax.dist = 12
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:83: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
      ax.w_xaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:84: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
      ax.w_yaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:85: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
      ax.w_zaxis.set_ticklabels([])
    /build/scikit-learn-GiwEwR/scikit-learn-1.1.2+dfsg/examples/cluster/plot_cluster_iris.py:90: MatplotlibDeprecationWarning: The dist attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.
      ax.dist = 12






|

.. code-block:: default


    # Code source: Gaël Varoquaux
    # Modified for documentation by Jaques Grobler
    # License: BSD 3 clause

    import numpy as np
    import matplotlib.pyplot as plt

    # Though the following import is not directly being used, it is required
    # for 3D projection to work with matplotlib < 3.2
    import mpl_toolkits.mplot3d  # noqa: F401

    from sklearn.cluster import KMeans
    from sklearn import datasets

    np.random.seed(5)

    iris = datasets.load_iris()
    X = iris.data
    y = iris.target

    estimators = [
        ("k_means_iris_8", KMeans(n_clusters=8)),
        ("k_means_iris_3", KMeans(n_clusters=3)),
        ("k_means_iris_bad_init", KMeans(n_clusters=3, n_init=1, init="random")),
    ]

    fignum = 1
    titles = ["8 clusters", "3 clusters", "3 clusters, bad initialization"]
    for name, est in estimators:
        fig = plt.figure(fignum, figsize=(4, 3))
        ax = fig.add_subplot(111, projection="3d", elev=48, azim=134)
        ax.set_position([0, 0, 0.95, 1])
        est.fit(X)
        labels = est.labels_

        ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=labels.astype(float), edgecolor="k")

        ax.w_xaxis.set_ticklabels([])
        ax.w_yaxis.set_ticklabels([])
        ax.w_zaxis.set_ticklabels([])
        ax.set_xlabel("Petal width")
        ax.set_ylabel("Sepal length")
        ax.set_zlabel("Petal length")
        ax.set_title(titles[fignum - 1])
        ax.dist = 12
        fignum = fignum + 1

    # Plot the ground truth
    fig = plt.figure(fignum, figsize=(4, 3))
    ax = fig.add_subplot(111, projection="3d", elev=48, azim=134)
    ax.set_position([0, 0, 0.95, 1])

    for name, label in [("Setosa", 0), ("Versicolour", 1), ("Virginica", 2)]:
        ax.text3D(
            X[y == label, 3].mean(),
            X[y == label, 0].mean(),
            X[y == label, 2].mean() + 2,
            name,
            horizontalalignment="center",
            bbox=dict(alpha=0.2, edgecolor="w", facecolor="w"),
        )
    # Reorder the labels to have colors matching the cluster results
    y = np.choose(y, [1, 2, 0]).astype(float)
    ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=y, edgecolor="k")

    ax.w_xaxis.set_ticklabels([])
    ax.w_yaxis.set_ticklabels([])
    ax.w_zaxis.set_ticklabels([])
    ax.set_xlabel("Petal width")
    ax.set_ylabel("Sepal length")
    ax.set_zlabel("Petal length")
    ax.set_title("Ground Truth")
    ax.dist = 12

    fig.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.269 seconds)


.. _sphx_glr_download_auto_examples_cluster_plot_cluster_iris.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: plot_cluster_iris.py <plot_cluster_iris.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: plot_cluster_iris.ipynb <plot_cluster_iris.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
