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

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_auto_examples_semi_supervised_plot_label_propagation_structure.py>`
        to download the full example code or to run this example in your browser via JupyterLite or Binder

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

.. _sphx_glr_auto_examples_semi_supervised_plot_label_propagation_structure.py:


==============================================
Label Propagation learning a complex structure
==============================================

Example of LabelPropagation learning a complex internal structure
to demonstrate "manifold learning". The outer circle should be
labeled "red" and the inner circle "blue". Because both label groups
lie inside their own distinct shape, we can see that the labels
propagate correctly around the circle.

.. GENERATED FROM PYTHON SOURCE LINES 13-18

.. code-block:: default


    # Authors: Clay Woolam <clay@woolam.org>
    #          Andreas Mueller <amueller@ais.uni-bonn.de>
    # License: BSD








.. GENERATED FROM PYTHON SOURCE LINES 19-23

We generate a dataset with two concentric circles. In addition, a label
is associated with each sample of the dataset that is: 0 (belonging to
the outer circle), 1 (belonging to the inner circle), and -1 (unknown).
Here, all labels but two are tagged as unknown.

.. GENERATED FROM PYTHON SOURCE LINES 23-35

.. code-block:: default


    import numpy as np

    from sklearn.datasets import make_circles

    n_samples = 200
    X, y = make_circles(n_samples=n_samples, shuffle=False)
    outer, inner = 0, 1
    labels = np.full(n_samples, -1.0)
    labels[0] = outer
    labels[-1] = inner








.. GENERATED FROM PYTHON SOURCE LINES 36-37

Plot raw data

.. GENERATED FROM PYTHON SOURCE LINES 37-68

.. code-block:: default

    import matplotlib.pyplot as plt

    plt.figure(figsize=(4, 4))
    plt.scatter(
        X[labels == outer, 0],
        X[labels == outer, 1],
        color="navy",
        marker="s",
        lw=0,
        label="outer labeled",
        s=10,
    )
    plt.scatter(
        X[labels == inner, 0],
        X[labels == inner, 1],
        color="c",
        marker="s",
        lw=0,
        label="inner labeled",
        s=10,
    )
    plt.scatter(
        X[labels == -1, 0],
        X[labels == -1, 1],
        color="darkorange",
        marker=".",
        label="unlabeled",
    )
    plt.legend(scatterpoints=1, shadow=False, loc="center")
    _ = plt.title("Raw data (2 classes=outer and inner)")




.. image-sg:: /auto_examples/semi_supervised/images/sphx_glr_plot_label_propagation_structure_001.png
   :alt: Raw data (2 classes=outer and inner)
   :srcset: /auto_examples/semi_supervised/images/sphx_glr_plot_label_propagation_structure_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 69-71

The aim of :class:`~sklearn.semi_supervised.LabelSpreading` is to associate
a label to sample where the label is initially unknown.

.. GENERATED FROM PYTHON SOURCE LINES 72-77

.. code-block:: default

    from sklearn.semi_supervised import LabelSpreading

    label_spread = LabelSpreading(kernel="knn", alpha=0.8)
    label_spread.fit(X, labels)






.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <style>#sk-container-id-65 {color: black;}#sk-container-id-65 pre{padding: 0;}#sk-container-id-65 div.sk-toggleable {background-color: white;}#sk-container-id-65 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-65 label.sk-toggleable__label-arrow:before {content: "▸";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-65 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-65 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-65 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-65 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-65 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-65 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: "▾";}#sk-container-id-65 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-65 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-65 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-65 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-65 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-65 div.sk-parallel-item::after {content: "";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-65 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-65 div.sk-serial::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-65 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-65 div.sk-item {position: relative;z-index: 1;}#sk-container-id-65 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-65 div.sk-item::before, #sk-container-id-65 div.sk-parallel-item::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-65 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-65 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-65 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-65 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-65 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-65 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-65 div.sk-label-container {text-align: center;}#sk-container-id-65 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-65 div.sk-text-repr-fallback {display: none;}</style><div id="sk-container-id-65" class="sk-top-container"><div class="sk-text-repr-fallback"><pre>LabelSpreading(alpha=0.8, kernel=&#x27;knn&#x27;)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class="sk-container" hidden><div class="sk-item"><div class="sk-estimator sk-toggleable"><input class="sk-toggleable__control sk-hidden--visually" id="sk-estimator-id-290" type="checkbox" checked><label for="sk-estimator-id-290" class="sk-toggleable__label sk-toggleable__label-arrow">LabelSpreading</label><div class="sk-toggleable__content"><pre>LabelSpreading(alpha=0.8, kernel=&#x27;knn&#x27;)</pre></div></div></div></div></div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 78-80

Now, we can check which labels have been associated with each sample
when the label was unknown.

.. GENERATED FROM PYTHON SOURCE LINES 80-107

.. code-block:: default

    output_labels = label_spread.transduction_
    output_label_array = np.asarray(output_labels)
    outer_numbers = np.where(output_label_array == outer)[0]
    inner_numbers = np.where(output_label_array == inner)[0]

    plt.figure(figsize=(4, 4))
    plt.scatter(
        X[outer_numbers, 0],
        X[outer_numbers, 1],
        color="navy",
        marker="s",
        lw=0,
        s=10,
        label="outer learned",
    )
    plt.scatter(
        X[inner_numbers, 0],
        X[inner_numbers, 1],
        color="c",
        marker="s",
        lw=0,
        s=10,
        label="inner learned",
    )
    plt.legend(scatterpoints=1, shadow=False, loc="center")
    plt.title("Labels learned with Label Spreading (KNN)")
    plt.show()



.. image-sg:: /auto_examples/semi_supervised/images/sphx_glr_plot_label_propagation_structure_002.png
   :alt: Labels learned with Label Spreading (KNN)
   :srcset: /auto_examples/semi_supervised/images/sphx_glr_plot_label_propagation_structure_002.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_semi_supervised_plot_label_propagation_structure.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/1.3.X?urlpath=lab/tree/notebooks/auto_examples/semi_supervised/plot_label_propagation_structure.ipynb
        :alt: Launch binder
        :width: 150 px



    .. container:: lite-badge

      .. image:: images/jupyterlite_badge_logo.svg
        :target: ../../lite/lab/?path=auto_examples/semi_supervised/plot_label_propagation_structure.ipynb
        :alt: Launch JupyterLite
        :width: 150 px

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

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

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

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


.. only:: html

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

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