Permalink
Browse files

Added MDS to manifold comparison methods

  • Loading branch information...
NelleV authored and GaelVaroquaux committed May 30, 2012
1 parent c4d2fe0 commit 7a0511c0b9b84fdcb959bf0c25293f1ce27eddaf
Showing with 32 additions and 6 deletions.
  1. +18 −4 examples/manifold/plot_compare_methods.py
  2. +14 −2 examples/manifold/plot_lle_digits.py
@@ -21,6 +21,7 @@
from matplotlib.ticker import NullFormatter
from sklearn import manifold, datasets
+from sklearn.metrics import euclidean_distances
# Next line to silence pyflakes. This import is needed.
Axes3D
@@ -36,11 +37,11 @@
try:
# compatibility matplotlib < 1.0
- ax = fig.add_subplot(231, projection='3d')
+ ax = fig.add_subplot(241, projection='3d')
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=pl.cm.Spectral)
ax.view_init(4, -72)
except:
- ax = fig.add_subplot(231, projection='3d')
+ ax = fig.add_subplot(241, projection='3d')
pl.scatter(X[:, 0], X[:, 2], c=color, cmap=pl.cm.Spectral)
methods = ['standard', 'ltsa', 'hessian', 'modified']
@@ -54,7 +55,7 @@
t1 = time()
print "%s: %.2g sec" % (methods[i], t1 - t0)
- ax = fig.add_subplot(232 + i)
+ ax = fig.add_subplot(242 + i)
pl.scatter(Y[:, 0], Y[:, 1], c=color, cmap=pl.cm.Spectral)
pl.title("%s (%.2g sec)" % (labels[i], t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
@@ -65,11 +66,24 @@
Y = manifold.Isomap(n_neighbors, n_components).fit_transform(X)
t1 = time()
print "Isomap: %.2g sec" % (t1 - t0)
-ax = fig.add_subplot(236)
+ax = fig.add_subplot(246)
pl.scatter(Y[:, 0], Y[:, 1], c=color, cmap=pl.cm.Spectral)
pl.title("Isomap (%.2g sec)" % (t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
pl.axis('tight')
+
+t0 = time()
+Y = manifold.MDS(n_components).fit_transform(euclidean_distances(X))
+t1 = time()
+print "MDS: %.2g sec" % (t1 - t0)
+ax = fig.add_subplot(247)
+pl.scatter(Y[:, 0], Y[:, 1], c=color, cmap=pl.cm.Spectral)
+pl.title("MDS (%.2g sec)" % (t1 - t0))
+ax.xaxis.set_major_formatter(NullFormatter())
+ax.yaxis.set_major_formatter(NullFormatter())
+pl.axis('tight')
+
+
pl.show()
@@ -19,6 +19,7 @@
from matplotlib import offsetbox
from sklearn.utils.fixes import qr_economic
from sklearn import manifold, datasets, decomposition, lda
+from sklearn.metrics import euclidean_distances
digits = datasets.load_digits(n_class=6)
X = digits.data
@@ -35,9 +36,9 @@ def plot_embedding(X, title=None):
pl.figure()
ax = pl.subplot(111)
- for i in range(digits.data.shape[0]):
+ for i in range(X.shape[0]):
pl.text(X[i, 0], X[i, 1], str(digits.target[i]),
- color=pl.cm.Set1(digits.target[i] / 10.),
+ color=pl.cm.Set1(y[i] / 10.),
fontdict={'weight': 'bold', 'size': 9})
if hasattr(offsetbox, 'AnnotationBbox'):
@@ -167,4 +168,15 @@ def plot_embedding(X, title=None):
"Local Tangent Space Alignment of the digits (time %.2fs)" %
(time() - t0))
+#----------------------------------------------------------------------
+# MDS embedding of the digits dataset
+print "Computing MDS embedding"
+clf = manifold.MDS(n_components=2, n_init=2)
+t0 = time()
+X_mds = clf.fit_transform(euclidean_distances(X))
+print "Done. Stress: %f" % clf.stress_
+plot_embedding(X_mds,
+ "MDS embedding of the digits (time %.2fs)" %
+ (time() - t0))
+
pl.show()

0 comments on commit 7a0511c

Please sign in to comment.