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.
|
2011-06-15 05:02:16 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Author: Jake Vanderplas -- <vanderplas@astro.washington.edu>
|
|
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
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
|
2012-12-09 01:43:16 +08:00
|
|
|
X, color = datasets.samples_generator.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
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
fig = plt.figure(figsize=(15, 8))
|
|
|
|
|
plt.suptitle("Manifold Learning with %i points, %i neighbors"
|
2014-05-15 10:35:13 +08:00
|
|
|
% (1000, n_neighbors), fontsize=14)
|
2011-06-15 05:02:16 +08:00
|
|
|
|
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
|
|
|
|
2012-12-09 01:02:25 +08:00
|
|
|
methods = ['standard', 'ltsa', 'hessian', 'modified']
|
2011-09-07 05:12:26 +08:00
|
|
|
labels = ['LLE', 'LTSA', 'Hessian LLE', 'Modified LLE']
|
2011-06-15 05:02:16 +08:00
|
|
|
|
2011-06-15 05:13:14 +08:00
|
|
|
for i, method in enumerate(methods):
|
2011-06-15 05:02:16 +08:00
|
|
|
t0 = time()
|
2012-05-06 19:00:39 +08:00
|
|
|
Y = manifold.LocallyLinearEmbedding(n_neighbors, n_components,
|
2011-08-12 10:13:33 +08:00
|
|
|
eigen_solver='auto',
|
2011-08-11 01:58:51 +08:00
|
|
|
method=method).fit_transform(X)
|
2011-06-15 05:02:16 +08:00
|
|
|
t1 = time()
|
2013-02-01 22:04:03 +08:00
|
|
|
print("%s: %.2g sec" % (methods[i], t1 - t0))
|
2011-06-15 05:13:14 +08:00
|
|
|
|
2014-02-08 07:58:58 +08:00
|
|
|
ax = fig.add_subplot(252 + i)
|
2014-05-29 02:04:46 +08:00
|
|
|
plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral)
|
|
|
|
|
plt.title("%s (%.2g sec)" % (labels[i], t1 - t0))
|
2011-06-29 11:14:42 +08:00
|
|
|
ax.xaxis.set_major_formatter(NullFormatter())
|
|
|
|
|
ax.yaxis.set_major_formatter(NullFormatter())
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.axis('tight')
|
2011-06-15 05:02:16 +08:00
|
|
|
|
2011-07-29 11:53:11 +08:00
|
|
|
t0 = time()
|
2012-05-06 19:00:39 +08:00
|
|
|
Y = manifold.Isomap(n_neighbors, n_components).fit_transform(X)
|
2011-07-29 11:53:11 +08:00
|
|
|
t1 = time()
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Isomap: %.2g sec" % (t1 - t0))
|
2014-02-08 07:58:58 +08:00
|
|
|
ax = fig.add_subplot(257)
|
2014-05-29 02:04:46 +08:00
|
|
|
plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral)
|
|
|
|
|
plt.title("Isomap (%.2g sec)" % (t1 - t0))
|
2011-07-29 11:53:11 +08:00
|
|
|
ax.xaxis.set_major_formatter(NullFormatter())
|
|
|
|
|
ax.yaxis.set_major_formatter(NullFormatter())
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.axis('tight')
|
2011-07-29 11:53:11 +08:00
|
|
|
|
2012-05-30 20:05:50 +08:00
|
|
|
|
|
|
|
|
t0 = time()
|
2012-06-04 04:40:05 +08:00
|
|
|
mds = manifold.MDS(n_components, max_iter=100, n_init=1)
|
2012-12-09 01:01:10 +08:00
|
|
|
Y = mds.fit_transform(X)
|
2012-05-30 20:05:50 +08:00
|
|
|
t1 = time()
|
2013-02-01 22:04:03 +08:00
|
|
|
print("MDS: %.2g sec" % (t1 - t0))
|
2014-02-08 07:58:58 +08:00
|
|
|
ax = fig.add_subplot(258)
|
2014-05-29 02:04:46 +08:00
|
|
|
plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral)
|
|
|
|
|
plt.title("MDS (%.2g sec)" % (t1 - t0))
|
2012-05-30 20:05:50 +08:00
|
|
|
ax.xaxis.set_major_formatter(NullFormatter())
|
|
|
|
|
ax.yaxis.set_major_formatter(NullFormatter())
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.axis('tight')
|
2012-05-30 20:05:50 +08:00
|
|
|
|
|
|
|
|
|
2012-10-07 19:07:38 +08:00
|
|
|
t0 = time()
|
2012-10-08 16:40:25 +08:00
|
|
|
se = manifold.SpectralEmbedding(n_components=n_components,
|
|
|
|
|
n_neighbors=n_neighbors)
|
2012-10-07 19:07:38 +08:00
|
|
|
Y = se.fit_transform(X)
|
|
|
|
|
t1 = time()
|
2013-02-01 22:04:03 +08:00
|
|
|
print("SpectralEmbedding: %.2g sec" % (t1 - t0))
|
2014-02-08 07:58:58 +08:00
|
|
|
ax = fig.add_subplot(259)
|
2014-05-29 02:04:46 +08:00
|
|
|
plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral)
|
|
|
|
|
plt.title("SpectralEmbedding (%.2g sec)" % (t1 - t0))
|
2012-10-07 19:07:38 +08:00
|
|
|
ax.xaxis.set_major_formatter(NullFormatter())
|
|
|
|
|
ax.yaxis.set_major_formatter(NullFormatter())
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.axis('tight')
|
2012-10-07 19:07:38 +08:00
|
|
|
|
2014-02-08 07:58:58 +08:00
|
|
|
t0 = time()
|
2014-05-21 03:58:31 +08:00
|
|
|
tsne = manifold.TSNE(n_components=n_components, init='pca', random_state=0)
|
2014-02-08 07:58:58 +08:00
|
|
|
Y = tsne.fit_transform(X)
|
|
|
|
|
t1 = time()
|
|
|
|
|
print("t-SNE: %.2g sec" % (t1 - t0))
|
2015-11-05 06:11:25 +08:00
|
|
|
ax = fig.add_subplot(2, 5, 10)
|
2014-05-29 02:04:46 +08:00
|
|
|
plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral)
|
|
|
|
|
plt.title("t-SNE (%.2g sec)" % (t1 - t0))
|
2014-02-08 07:58:58 +08:00
|
|
|
ax.xaxis.set_major_formatter(NullFormatter())
|
|
|
|
|
ax.yaxis.set_major_formatter(NullFormatter())
|
2014-05-29 02:04:46 +08:00
|
|
|
plt.axis('tight')
|
2014-02-08 07:58:58 +08:00
|
|
|
|
2014-05-29 02:04:46 +08:00
|
|
|
plt.show()
|