2011-01-31 23:51:38 +08:00
|
|
|
"""
|
|
|
|
|
===================================
|
|
|
|
|
Swiss Roll reduction with LLE
|
|
|
|
|
===================================
|
|
|
|
|
|
|
|
|
|
An illustration of Swiss Roll reduction
|
|
|
|
|
with locally linear embedding
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Author: Fabian Pedregosa -- <fabian.pedregosa@inria.fr>
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause (C) INRIA 2011
|
2011-01-31 23:51:38 +08:00
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-01-31 23:51:38 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2012-04-28 18:04:36 +08:00
|
|
|
|
2011-05-15 21:53:25 +08:00
|
|
|
# This import is needed to modify the way figure behaves
|
|
|
|
|
from mpl_toolkits.mplot3d import Axes3D
|
2012-04-28 18:04:36 +08:00
|
|
|
Axes3D
|
2011-01-31 23:51:38 +08:00
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
# Locally linear embedding of the swiss roll
|
|
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import manifold, datasets
|
2011-08-04 21:50:19 +08:00
|
|
|
X, color = datasets.samples_generator.make_swiss_roll(n_samples=1500)
|
2011-01-31 23:51:38 +08:00
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Computing LLE embedding")
|
2012-05-06 22:07:22 +08:00
|
|
|
X_r, err = manifold.locally_linear_embedding(X, n_neighbors=12,
|
|
|
|
|
n_components=2)
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Done. Reconstruction error: %g" % err)
|
2011-01-31 23:51:38 +08:00
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
# Plot result
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
fig = plt.figure()
|
2011-05-13 22:08:55 +08:00
|
|
|
try:
|
|
|
|
|
# compatibility matplotlib < 1.0
|
|
|
|
|
ax = fig.add_subplot(211, projection='3d')
|
2014-05-15 04:31:03 +08:00
|
|
|
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=plt.cm.Spectral)
|
2011-05-13 22:08:55 +08:00
|
|
|
except:
|
|
|
|
|
ax = fig.add_subplot(211)
|
2014-05-15 04:31:03 +08:00
|
|
|
ax.scatter(X[:, 0], X[:, 2], c=color, cmap=plt.cm.Spectral)
|
2011-05-13 22:08:55 +08:00
|
|
|
|
2011-01-31 23:51:38 +08:00
|
|
|
ax.set_title("Original data")
|
|
|
|
|
ax = fig.add_subplot(212)
|
2014-05-15 04:31:03 +08:00
|
|
|
ax.scatter(X_r[:, 0], X_r[:, 1], c=color, cmap=plt.cm.Spectral)
|
|
|
|
|
plt.axis('tight')
|
|
|
|
|
plt.xticks([]), plt.yticks([])
|
|
|
|
|
plt.title('Projected data')
|
|
|
|
|
plt.show()
|