{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n=============================================================================\nt-SNE: The effect of various perplexity values on the shape\n=============================================================================\n\nAn illustration of t-SNE on the two concentric circles and the S-curve\ndatasets for different perplexity values.\n\nWe observe a tendency towards clearer shapes as the preplexity value increases.\n\nThe size, the distance and the shape of clusters may vary upon initialization,\nperplexity values and does not always convey a meaning.\n\nAs shown below, t-SNE for higher perplexities finds meaningful topology of\ntwo concentric circles, however the size and the distance of the circles varies\nslightly from the original. Contrary to the two circles dataset, the shapes\nvisually diverge from S-curve topology on the S-curve dataset even for\nlarger perplexity values.\n\nFor further details, \"How to Use t-SNE Effectively\"\nhttp://distill.pub/2016/misread-tsne/ provides a good discussion of the\neffects of various parameters, as well as interactive plots to explore\nthose effects.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Author: Narine Kokhlikyan <narine@slice.com>\n# License: BSD\n\nprint(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom matplotlib.ticker import NullFormatter\nfrom sklearn import manifold, datasets\nfrom time import time\n\nn_samples = 300\nn_components = 2\n(fig, subplots) = plt.subplots(3, 5, figsize=(15, 8))\nperplexities = [5, 30, 50, 100]\n\nX, y = datasets.make_circles(n_samples=n_samples, factor=.5, noise=.05)\n\nred = y == 0\ngreen = y == 1\n\nax = subplots[0][0]\nax.scatter(X[red, 0], X[red, 1], c=\"r\")\nax.scatter(X[green, 0], X[green, 1], c=\"g\")\nax.xaxis.set_major_formatter(NullFormatter())\nax.yaxis.set_major_formatter(NullFormatter())\nplt.axis('tight')\n\nfor i, perplexity in enumerate(perplexities):\n    ax = subplots[0][i + 1]\n\n    t0 = time()\n    tsne = manifold.TSNE(n_components=n_components, init='random',\n                         random_state=0, perplexity=perplexity)\n    Y = tsne.fit_transform(X)\n    t1 = time()\n    print(\"circles, perplexity=%d in %.2g sec\" % (perplexity, t1 - t0))\n    ax.set_title(\"Perplexity=%d\" % perplexity)\n    ax.scatter(Y[red, 0], Y[red, 1], c=\"r\")\n    ax.scatter(Y[green, 0], Y[green, 1], c=\"g\")\n    ax.xaxis.set_major_formatter(NullFormatter())\n    ax.yaxis.set_major_formatter(NullFormatter())\n    ax.axis('tight')\n\n# Another example using s-curve\nX, color = datasets.samples_generator.make_s_curve(n_samples, random_state=0)\n\nax = subplots[1][0]\nax.scatter(X[:, 0], X[:, 2], c=color)\nax.xaxis.set_major_formatter(NullFormatter())\nax.yaxis.set_major_formatter(NullFormatter())\n\nfor i, perplexity in enumerate(perplexities):\n    ax = subplots[1][i + 1]\n\n    t0 = time()\n    tsne = manifold.TSNE(n_components=n_components, init='random',\n                         random_state=0, perplexity=perplexity)\n    Y = tsne.fit_transform(X)\n    t1 = time()\n    print(\"S-curve, perplexity=%d in %.2g sec\" % (perplexity, t1 - t0))\n\n    ax.set_title(\"Perplexity=%d\" % perplexity)\n    ax.scatter(Y[:, 0], Y[:, 1], c=color)\n    ax.xaxis.set_major_formatter(NullFormatter())\n    ax.yaxis.set_major_formatter(NullFormatter())\n    ax.axis('tight')\n\n\n# Another example using a 2D uniform grid\nx = np.linspace(0, 1, int(np.sqrt(n_samples)))\nxx, yy = np.meshgrid(x, x)\nX = np.hstack([\n    xx.ravel().reshape(-1, 1),\n    yy.ravel().reshape(-1, 1),\n])\ncolor = xx.ravel()\nax = subplots[2][0]\nax.scatter(X[:, 0], X[:, 1], c=color)\nax.xaxis.set_major_formatter(NullFormatter())\nax.yaxis.set_major_formatter(NullFormatter())\n\nfor i, perplexity in enumerate(perplexities):\n    ax = subplots[2][i + 1]\n\n    t0 = time()\n    tsne = manifold.TSNE(n_components=n_components, init='random',\n                         random_state=0, perplexity=perplexity)\n    Y = tsne.fit_transform(X)\n    t1 = time()\n    print(\"uniform grid, perplexity=%d in %.2g sec\" % (perplexity, t1 - t0))\n\n    ax.set_title(\"Perplexity=%d\" % perplexity)\n    ax.scatter(Y[:, 0], Y[:, 1], c=color)\n    ax.xaxis.set_major_formatter(NullFormatter())\n    ax.yaxis.set_major_formatter(NullFormatter())\n    ax.axis('tight')\n\n\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
}