{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Comparison of Manifold Learning methods\n\nAn illustration of dimensionality reduction on the S-curve dataset\nwith various manifold learning methods.\n\nFor a discussion and comparison of these algorithms, see the\n`manifold module page `\n\nFor a similar example, where the methods are applied to a\nsphere dataset, see `sphx_glr_auto_examples_manifold_plot_manifold_sphere.py`\n\nNote that the purpose of the MDS is to find a low-dimensional\nrepresentation of the data (here 2D) in which the distances respect well\nthe distances in the original high-dimensional space, unlike other\nmanifold-learning algorithms, it does not seeks an isotropic\nrepresentation of the data in the low-dimensional space.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Jake Vanderplas -- \n\nprint(__doc__)\n\nfrom collections import OrderedDict\nfrom functools import partial\nfrom time import time\n\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom matplotlib.ticker import NullFormatter\n\nfrom sklearn import manifold, datasets\n\n# Next line to silence pyflakes. This import is needed.\nAxes3D\n\nn_points = 1000\nX, color = datasets.make_s_curve(n_points, random_state=0)\nn_neighbors = 10\nn_components = 2\n\n# Create figure\nfig = plt.figure(figsize=(15, 8))\nfig.suptitle(\n \"Manifold Learning with %i points, %i neighbors\" % (1000, n_neighbors), fontsize=14\n)\n\n# Add 3d scatter plot\nax = fig.add_subplot(251, projection=\"3d\")\nax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=plt.cm.Spectral)\nax.view_init(4, -72)\n\n# Set-up manifold methods\nLLE = partial(\n manifold.LocallyLinearEmbedding,\n n_neighbors=n_neighbors,\n n_components=n_components,\n eigen_solver=\"auto\",\n)\n\nmethods = OrderedDict()\nmethods[\"LLE\"] = LLE(method=\"standard\")\nmethods[\"LTSA\"] = LLE(method=\"ltsa\")\nmethods[\"Hessian LLE\"] = LLE(method=\"hessian\")\nmethods[\"Modified LLE\"] = LLE(method=\"modified\")\nmethods[\"Isomap\"] = manifold.Isomap(n_neighbors=n_neighbors, n_components=n_components)\nmethods[\"MDS\"] = manifold.MDS(n_components, max_iter=100, n_init=1)\nmethods[\"SE\"] = manifold.SpectralEmbedding(\n n_components=n_components, n_neighbors=n_neighbors\n)\nmethods[\"t-SNE\"] = manifold.TSNE(n_components=n_components, init=\"pca\", random_state=0)\n\n# Plot results\nfor i, (label, method) in enumerate(methods.items()):\n t0 = time()\n Y = method.fit_transform(X)\n t1 = time()\n print(\"%s: %.2g sec\" % (label, t1 - t0))\n ax = fig.add_subplot(2, 5, 2 + i + (i > 3))\n ax.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral)\n ax.set_title(\"%s (%.2g sec)\" % (label, t1 - t0))\n ax.xaxis.set_major_formatter(NullFormatter())\n ax.yaxis.set_major_formatter(NullFormatter())\n ax.axis(\"tight\")\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.9.7" } }, "nbformat": 4, "nbformat_minor": 0 }