2011-08-14 01:06:52 +08:00
|
|
|
"""
|
2011-06-15 05:02:16 +08:00
|
|
|
=========================================
|
2018-06-18 14:07:33 +08:00
|
|
|
Comparison of Manifold Learning methods
|
2011-06-15 05:02:16 +08:00
|
|
|
=========================================
|
|
|
|
|
|
2011-07-29 11:53:11 +08:00
|
|
|
An illustration of dimensionality reduction on the S-curve dataset
|
2011-08-09 08:39:15 +08:00
|
|
|
with various manifold learning methods.
|
2011-07-29 11:53:11 +08:00
|
|
|
|
|
|
|
|
For a discussion and comparison of these algorithms, see the
|
|
|
|
|
:ref:`manifold module page <manifold>`
|
2012-06-02 00:12:00 +08:00
|
|
|
|
2013-04-12 02:51:28 +08:00
|
|
|
For a similar example, where the methods are applied to a
|
2016-01-25 07:18:26 +08:00
|
|
|
sphere dataset, see :ref:`sphx_glr_auto_examples_manifold_plot_manifold_sphere.py`
|
2012-11-20 22:47:02 +08:00
|
|
|
|
2012-06-02 00:12:00 +08:00
|
|
|
Note that the purpose of the MDS is to find a low-dimensional
|
|
|
|
|
representation of the data (here 2D) in which the distances respect well
|
|
|
|
|
the distances in the original high-dimensional space, unlike other
|
|
|
|
|
manifold-learning algorithms, it does not seeks an isotropic
|
|
|
|
|
representation of the data in the low-dimensional space.
|
2021-10-22 21:33:22 +08:00
|
|
|
|
2011-06-15 05:02:16 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Author: Jake Vanderplas -- <vanderplas@astro.washington.edu>
|
|
|
|
|
|
2019-12-26 20:02:31 +08:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
from functools import partial
|
2011-06-15 05:02:16 +08:00
|
|
|
from time import time
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-06-15 05:02:16 +08:00
|
|
|
from mpl_toolkits.mplot3d import Axes3D
|
2011-06-29 11:14:42 +08:00
|
|
|
from matplotlib.ticker import NullFormatter
|
2011-06-15 05:02:16 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import manifold, datasets
|
2011-06-15 05:02:16 +08:00
|
|
|
|
2012-04-28 18:04:36 +08:00
|
|
|
# Next line to silence pyflakes. This import is needed.
|
|
|
|
|
Axes3D
|
|
|
|
|
|
2011-08-09 08:39:15 +08:00
|
|
|
n_points = 1000
|
2019-10-28 05:17:23 +08:00
|
|
|
X, color = datasets.make_s_curve(n_points, random_state=0)
|
2011-08-12 10:13:33 +08:00
|
|
|
n_neighbors = 10
|
2012-05-06 19:00:39 +08:00
|
|
|
n_components = 2
|
2011-07-29 08:47:08 +08:00
|
|
|
|
2019-12-26 20:02:31 +08:00
|
|
|
# Create figure
|
2014-05-15 04:31:03 +08:00
|
|
|
fig = plt.figure(figsize=(15, 8))
|
2019-12-26 20:02:31 +08:00
|
|
|
fig.suptitle(
|
|
|
|
|
"Manifold Learning with %i points, %i neighbors" % (1000, n_neighbors), fontsize=14
|
2014-05-15 10:35:13 +08:00
|
|
|
)
|
2011-06-15 05:02:16 +08:00
|
|
|
|
2019-12-26 20:02:31 +08:00
|
|
|
# Add 3d scatter plot
|
2017-03-10 15:25:37 +08:00
|
|
|
ax = fig.add_subplot(251, projection="3d")
|
|
|
|
|
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=plt.cm.Spectral)
|
|
|
|
|
ax.view_init(4, -72)
|
2011-06-15 05:02:16 +08:00
|
|
|
|
2019-12-26 20:02:31 +08:00
|
|
|
# Set-up manifold methods
|
|
|
|
|
LLE = partial(
|
|
|
|
|
manifold.LocallyLinearEmbedding,
|
2021-05-14 23:30:27 +08:00
|
|
|
n_neighbors=n_neighbors,
|
|
|
|
|
n_components=n_components,
|
|
|
|
|
eigen_solver="auto",
|
|
|
|
|
)
|
2019-12-26 20:02:31 +08:00
|
|
|
|
|
|
|
|
methods = OrderedDict()
|
|
|
|
|
methods["LLE"] = LLE(method="standard")
|
|
|
|
|
methods["LTSA"] = LLE(method="ltsa")
|
|
|
|
|
methods["Hessian LLE"] = LLE(method="hessian")
|
|
|
|
|
methods["Modified LLE"] = LLE(method="modified")
|
2021-05-14 23:30:27 +08:00
|
|
|
methods["Isomap"] = manifold.Isomap(n_neighbors=n_neighbors, n_components=n_components)
|
2019-12-26 20:02:31 +08:00
|
|
|
methods["MDS"] = manifold.MDS(n_components, max_iter=100, n_init=1)
|
|
|
|
|
methods["SE"] = manifold.SpectralEmbedding(
|
|
|
|
|
n_components=n_components, n_neighbors=n_neighbors
|
|
|
|
|
)
|
|
|
|
|
methods["t-SNE"] = manifold.TSNE(n_components=n_components, init="pca", random_state=0)
|
|
|
|
|
|
|
|
|
|
# Plot results
|
|
|
|
|
for i, (label, method) in enumerate(methods.items()):
|
2011-06-15 05:02:16 +08:00
|
|
|
t0 = time()
|
2019-12-26 20:02:31 +08:00
|
|
|
Y = method.fit_transform(X)
|
2011-06-15 05:02:16 +08:00
|
|
|
t1 = time()
|
2019-12-26 20:02:31 +08:00
|
|
|
print("%s: %.2g sec" % (label, t1 - t0))
|
|
|
|
|
ax = fig.add_subplot(2, 5, 2 + i + (i > 3))
|
|
|
|
|
ax.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral)
|
|
|
|
|
ax.set_title("%s (%.2g sec)" % (label, t1 - t0))
|
2011-06-29 11:14:42 +08:00
|
|
|
ax.xaxis.set_major_formatter(NullFormatter())
|
|
|
|
|
ax.yaxis.set_major_formatter(NullFormatter())
|
2019-12-26 20:02:31 +08:00
|
|
|
ax.axis("tight")
|
2014-02-08 07:58:58 +08:00
|
|
|
|
2014-05-29 02:04:46 +08:00
|
|
|
plt.show()
|