{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Swiss Roll And Swiss-Hole Reduction\nThis notebook seeks to compare two popular non-linear dimensionality\ntechniques, T-distributed Stochastic Neighbor Embedding (t-SNE) and\nLocally Linear Embedding (LLE), on the classic Swiss Roll dataset.\nThen, we will explore how they both deal with the addition of a hole\nin the data.\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": [ "## Swiss Roll\n\nWe start by generating the Swiss Roll dataset.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfrom sklearn import datasets, manifold\n\nsr_points, sr_color = datasets.make_swiss_roll(n_samples=1500, random_state=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's take a look at our data:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig = plt.figure(figsize=(8, 6))\nax = fig.add_subplot(111, projection=\"3d\")\nfig.add_axes(ax)\nax.scatter(\n sr_points[:, 0], sr_points[:, 1], sr_points[:, 2], c=sr_color, s=50, alpha=0.8\n)\nax.set_title(\"Swiss Roll in Ambient Space\")\nax.view_init(azim=-66, elev=12)\n_ = ax.text2D(0.8, 0.05, s=\"n_samples=1500\", transform=ax.transAxes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Computing the LLE and t-SNE embeddings, we find that LLE seems to unroll the\nSwiss Roll pretty effectively. t-SNE on the other hand, is able\nto preserve the general structure of the data, but, poorly represents the\ncontinuous nature of our original data. Instead, it seems to unnecessarily\nclump sections of points together.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sr_lle, sr_err = manifold.locally_linear_embedding(\n sr_points, n_neighbors=12, n_components=2\n)\n\nsr_tsne = manifold.TSNE(n_components=2, perplexity=40, random_state=0).fit_transform(\n sr_points\n)\n\nfig, axs = plt.subplots(figsize=(8, 8), nrows=2)\naxs[0].scatter(sr_lle[:, 0], sr_lle[:, 1], c=sr_color)\naxs[0].set_title(\"LLE Embedding of Swiss Roll\")\naxs[1].scatter(sr_tsne[:, 0], sr_tsne[:, 1], c=sr_color)\n_ = axs[1].set_title(\"t-SNE Embedding of Swiss Roll\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Note

LLE seems to be stretching the points from the center (purple)\n of the swiss roll. However, we observe that this is simply a byproduct\n of how the data was generated. There is a higher density of points near the\n center of the roll, which ultimately affects how LLE reconstructs the\n data in a lower dimension.

\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Swiss-Hole\n\nNow let's take a look at how both algorithms deal with us adding a hole to\nthe data. First, we generate the Swiss-Hole dataset and plot it:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sh_points, sh_color = datasets.make_swiss_roll(\n n_samples=1500, hole=True, random_state=0\n)\n\nfig = plt.figure(figsize=(8, 6))\nax = fig.add_subplot(111, projection=\"3d\")\nfig.add_axes(ax)\nax.scatter(\n sh_points[:, 0], sh_points[:, 1], sh_points[:, 2], c=sh_color, s=50, alpha=0.8\n)\nax.set_title(\"Swiss-Hole in Ambient Space\")\nax.view_init(azim=-66, elev=12)\n_ = ax.text2D(0.8, 0.05, s=\"n_samples=1500\", transform=ax.transAxes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Computing the LLE and t-SNE embeddings, we obtain similar results to the\nSwiss Roll. LLE very capably unrolls the data and even preserves\nthe hole. t-SNE, again seems to clump sections of points together, but, we\nnote that it preserves the general topology of the original data.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sh_lle, sh_err = manifold.locally_linear_embedding(\n sh_points, n_neighbors=12, n_components=2\n)\n\nsh_tsne = manifold.TSNE(\n n_components=2, perplexity=40, init=\"random\", random_state=0\n).fit_transform(sh_points)\n\nfig, axs = plt.subplots(figsize=(8, 8), nrows=2)\naxs[0].scatter(sh_lle[:, 0], sh_lle[:, 1], c=sh_color)\naxs[0].set_title(\"LLE Embedding of Swiss-Hole\")\naxs[1].scatter(sh_tsne[:, 0], sh_tsne[:, 1], c=sh_color)\n_ = axs[1].set_title(\"t-SNE Embedding of Swiss-Hole\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Concluding remarks\n\nWe note that t-SNE benefits from testing more combinations of parameters.\nBetter results could probably have been obtained by better tuning these\nparameters.\n\nWe observe that, as seen in the \"Manifold learning on\nhandwritten digits\" example, t-SNE generally performs better than LLE\non real world data.\n\n" ] } ], "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.9.21" } }, "nbformat": 4, "nbformat_minor": 0 }