Skip to content

Commit

Permalink
Added MDS to manifold comparison methods
Browse files Browse the repository at this point in the history
  • Loading branch information
NelleV authored and GaelVaroquaux committed Jun 1, 2012
1 parent c4d2fe0 commit 7a0511c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
22 changes: 18 additions & 4 deletions examples/manifold/plot_compare_methods.py
Expand Up @@ -21,6 +21,7 @@
from matplotlib.ticker import NullFormatter from matplotlib.ticker import NullFormatter


from sklearn import manifold, datasets from sklearn import manifold, datasets
from sklearn.metrics import euclidean_distances


# Next line to silence pyflakes. This import is needed. # Next line to silence pyflakes. This import is needed.
Axes3D Axes3D
Expand All @@ -36,11 +37,11 @@


try: try:
# compatibility matplotlib < 1.0 # 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.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=pl.cm.Spectral)
ax.view_init(4, -72) ax.view_init(4, -72)
except: 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) pl.scatter(X[:, 0], X[:, 2], c=color, cmap=pl.cm.Spectral)


methods = ['standard', 'ltsa', 'hessian', 'modified'] methods = ['standard', 'ltsa', 'hessian', 'modified']
Expand All @@ -54,7 +55,7 @@
t1 = time() t1 = time()
print "%s: %.2g sec" % (methods[i], t1 - t0) 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.scatter(Y[:, 0], Y[:, 1], c=color, cmap=pl.cm.Spectral)
pl.title("%s (%.2g sec)" % (labels[i], t1 - t0)) pl.title("%s (%.2g sec)" % (labels[i], t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter()) ax.xaxis.set_major_formatter(NullFormatter())
Expand All @@ -65,11 +66,24 @@
Y = manifold.Isomap(n_neighbors, n_components).fit_transform(X) Y = manifold.Isomap(n_neighbors, n_components).fit_transform(X)
t1 = time() t1 = time()
print "Isomap: %.2g sec" % (t1 - t0) 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.scatter(Y[:, 0], Y[:, 1], c=color, cmap=pl.cm.Spectral)
pl.title("Isomap (%.2g sec)" % (t1 - t0)) pl.title("Isomap (%.2g sec)" % (t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter()) ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter())
pl.axis('tight') 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() pl.show()
16 changes: 14 additions & 2 deletions examples/manifold/plot_lle_digits.py
Expand Up @@ -19,6 +19,7 @@
from matplotlib import offsetbox from matplotlib import offsetbox
from sklearn.utils.fixes import qr_economic from sklearn.utils.fixes import qr_economic
from sklearn import manifold, datasets, decomposition, lda from sklearn import manifold, datasets, decomposition, lda
from sklearn.metrics import euclidean_distances


digits = datasets.load_digits(n_class=6) digits = datasets.load_digits(n_class=6)
X = digits.data X = digits.data
Expand All @@ -35,9 +36,9 @@ def plot_embedding(X, title=None):


pl.figure() pl.figure()
ax = pl.subplot(111) 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]), 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}) fontdict={'weight': 'bold', 'size': 9})


if hasattr(offsetbox, 'AnnotationBbox'): if hasattr(offsetbox, 'AnnotationBbox'):
Expand Down Expand Up @@ -167,4 +168,15 @@ def plot_embedding(X, title=None):
"Local Tangent Space Alignment of the digits (time %.2fs)" % "Local Tangent Space Alignment of the digits (time %.2fs)" %
(time() - t0)) (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() pl.show()

0 comments on commit 7a0511c

Please sign in to comment.