{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Label Propagation circles: Learning a complex structure\n\nExample of LabelPropagation learning a complex internal structure\nto demonstrate \"manifold learning\". The outer circle should be\nlabeled \"red\" and the inner circle \"blue\". Because both label groups\nlie inside their own distinct shape, we can see that the labels\npropagate correctly around the circle.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We generate a dataset with two concentric circles. In addition, a label\nis associated with each sample of the dataset that is: 0 (belonging to\nthe outer circle), 1 (belonging to the inner circle), and -1 (unknown).\nHere, all labels but two are tagged as unknown.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nfrom sklearn.datasets import make_circles\n\nn_samples = 200\nX, y = make_circles(n_samples=n_samples, shuffle=False)\nouter, inner = 0, 1\nlabels = np.full(n_samples, -1.0)\nlabels[0] = outer\nlabels[-1] = inner"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot raw data\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nplt.figure(figsize=(4, 4))\nplt.scatter(\n    X[labels == outer, 0],\n    X[labels == outer, 1],\n    color=\"navy\",\n    marker=\"s\",\n    lw=0,\n    label=\"outer labeled\",\n    s=10,\n)\nplt.scatter(\n    X[labels == inner, 0],\n    X[labels == inner, 1],\n    color=\"c\",\n    marker=\"s\",\n    lw=0,\n    label=\"inner labeled\",\n    s=10,\n)\nplt.scatter(\n    X[labels == -1, 0],\n    X[labels == -1, 1],\n    color=\"darkorange\",\n    marker=\".\",\n    label=\"unlabeled\",\n)\nplt.legend(scatterpoints=1, shadow=False, loc=\"center\")\n_ = plt.title(\"Raw data (2 classes=outer and inner)\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The aim of :class:`~sklearn.semi_supervised.LabelSpreading` is to associate\na label to sample where the label is initially unknown.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.semi_supervised import LabelSpreading\n\nlabel_spread = LabelSpreading(kernel=\"knn\", alpha=0.8)\nlabel_spread.fit(X, labels)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, we can check which labels have been associated with each sample\nwhen the label was unknown.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "output_labels = label_spread.transduction_\noutput_label_array = np.asarray(output_labels)\nouter_numbers = (output_label_array == outer).nonzero()[0]\ninner_numbers = (output_label_array == inner).nonzero()[0]\n\nplt.figure(figsize=(4, 4))\nplt.scatter(\n    X[outer_numbers, 0],\n    X[outer_numbers, 1],\n    color=\"navy\",\n    marker=\"s\",\n    lw=0,\n    s=10,\n    label=\"outer learned\",\n)\nplt.scatter(\n    X[inner_numbers, 0],\n    X[inner_numbers, 1],\n    color=\"c\",\n    marker=\"s\",\n    lw=0,\n    s=10,\n    label=\"inner learned\",\n)\nplt.legend(scatterpoints=1, shadow=False, loc=\"center\")\nplt.title(\"Labels learned with Label Spreading (KNN)\")\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.14"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}