2011-08-14 01:06:52 +08:00
|
|
|
"""
|
2011-06-15 05:02:16 +08:00
|
|
|
=========================================
|
2011-07-29 11:53:11 +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>`
|
2011-06-15 05:02:16 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Author: Jake Vanderplas -- <vanderplas@astro.washington.edu>
|
|
|
|
|
|
|
|
|
|
print __doc__
|
|
|
|
|
|
|
|
|
|
from time import time
|
|
|
|
|
|
2011-08-11 06:28:16 +08:00
|
|
|
import pylab as pl
|
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
|
|
|
|
2011-08-09 08:39:15 +08:00
|
|
|
n_points = 1000
|
|
|
|
|
X, color = datasets.samples_generator.make_s_curve(n_points)
|
2011-08-12 10:13:33 +08:00
|
|
|
n_neighbors = 10
|
2011-07-29 10:38:18 +08:00
|
|
|
out_dim = 2
|
2011-07-29 08:47:08 +08:00
|
|
|
|
2011-08-11 06:28:16 +08:00
|
|
|
fig = pl.figure(figsize=(12, 8))
|
|
|
|
|
pl.suptitle("Manifold Learning with %i points, %i neighbors"
|
2011-08-09 01:35:14 +08:00
|
|
|
% (1000, n_neighbors), fontsize=14)
|
2011-06-15 05:02:16 +08:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# compatibility matplotlib < 1.0
|
2011-08-11 06:28:16 +08:00
|
|
|
ax = fig.add_subplot(231, projection='3d')
|
|
|
|
|
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=pl.cm.Spectral)
|
2011-06-15 05:02:16 +08:00
|
|
|
ax.view_init(4, -72)
|
|
|
|
|
except:
|
2011-08-11 06:28:16 +08:00
|
|
|
ax = fig.add_subplot(231, projection='3d')
|
|
|
|
|
pl.scatter(X[:, 0], X[:, 2], c=color, cmap=pl.cm.Spectral)
|
2011-06-15 05:02:16 +08:00
|
|
|
|
2011-08-09 08:39:15 +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()
|
2011-08-11 01:58:51 +08:00
|
|
|
Y = manifold.LocallyLinearEmbedding(n_neighbors, out_dim,
|
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()
|
|
|
|
|
print "%s: %.2g sec" % (methods[i], t1 - t0)
|
2011-06-15 05:13:14 +08:00
|
|
|
|
2011-08-11 06:28:16 +08:00
|
|
|
ax = fig.add_subplot(232 + i)
|
|
|
|
|
pl.scatter(Y[:, 0], Y[:, 1], c=color, cmap=pl.cm.Spectral)
|
|
|
|
|
pl.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())
|
2011-08-11 06:28:16 +08:00
|
|
|
pl.axis('tight')
|
2011-06-15 05:02:16 +08:00
|
|
|
|
2011-07-29 11:53:11 +08:00
|
|
|
t0 = time()
|
2011-08-05 12:28:40 +08:00
|
|
|
Y = manifold.Isomap(n_neighbors, out_dim).fit_transform(X)
|
2011-07-29 11:53:11 +08:00
|
|
|
t1 = time()
|
|
|
|
|
print "Isomap: %.2g sec" % (t1 - t0)
|
2011-08-11 06:28:16 +08:00
|
|
|
ax = fig.add_subplot(236)
|
|
|
|
|
pl.scatter(Y[:, 0], Y[:, 1], c=color, cmap=pl.cm.Spectral)
|
|
|
|
|
pl.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())
|
2011-08-14 01:06:52 +08:00
|
|
|
pl.axis('tight')
|
2011-07-29 11:53:11 +08:00
|
|
|
|
2011-08-11 06:28:16 +08:00
|
|
|
pl.show()
|