{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Label Propagation learning a complex structure\n\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\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\n# Authors: Clay Woolam <clay@woolam.org>\n#          Andreas Mueller <amueller@ais.uni-bonn.de>\n# License: BSD\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.semi_supervised import label_propagation\nfrom sklearn.datasets import make_circles\n\n# generate ring with inner box\nn_samples = 200\nX, y = make_circles(n_samples=n_samples, shuffle=False)\nouter, inner = 0, 1\nlabels = np.full(n_samples, -1.)\nlabels[0] = outer\nlabels[-1] = inner\n\n# #############################################################################\n# Learn with LabelSpreading\nlabel_spread = label_propagation.LabelSpreading(kernel='knn', alpha=0.8)\nlabel_spread.fit(X, labels)\n\n# #############################################################################\n# Plot output labels\noutput_labels = label_spread.transduction_\nplt.figure(figsize=(8.5, 4))\nplt.subplot(1, 2, 1)\nplt.scatter(X[labels == outer, 0], X[labels == outer, 1], color='navy',\n            marker='s', lw=0, label=\"outer labeled\", s=10)\nplt.scatter(X[labels == inner, 0], X[labels == inner, 1], color='c',\n            marker='s', lw=0, label='inner labeled', s=10)\nplt.scatter(X[labels == -1, 0], X[labels == -1, 1], color='darkorange',\n            marker='.', label='unlabeled')\nplt.legend(scatterpoints=1, shadow=False, loc='upper right')\nplt.title(\"Raw data (2 classes=outer and inner)\")\n\nplt.subplot(1, 2, 2)\noutput_label_array = np.asarray(output_labels)\nouter_numbers = np.where(output_label_array == outer)[0]\ninner_numbers = np.where(output_label_array == inner)[0]\nplt.scatter(X[outer_numbers, 0], X[outer_numbers, 1], color='navy',\n            marker='s', lw=0, s=10, label=\"outer learned\")\nplt.scatter(X[inner_numbers, 0], X[inner_numbers, 1], color='c',\n            marker='s', lw=0, s=10, label=\"inner learned\")\nplt.legend(scatterpoints=1, shadow=False, loc='upper right')\nplt.title(\"Labels learned with Label Spreading (KNN)\")\n\nplt.subplots_adjust(left=0.07, bottom=0.07, right=0.93, top=0.92)\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.6.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}