|
23 | 23 |
|
24 | 24 | print(__doc__)
|
25 | 25 |
|
| 26 | +from collections import OrderedDict |
| 27 | +from functools import partial |
26 | 28 | from time import time
|
27 | 29 |
|
28 | 30 | import matplotlib.pyplot as plt
|
|
39 | 41 | n_neighbors = 10
|
40 | 42 | n_components = 2
|
41 | 43 |
|
| 44 | +# Create figure |
42 | 45 | fig = plt.figure(figsize=(15, 8))
|
43 |
| -plt.suptitle("Manifold Learning with %i points, %i neighbors" |
| 46 | +fig.suptitle("Manifold Learning with %i points, %i neighbors" |
44 | 47 | % (1000, n_neighbors), fontsize=14)
|
45 | 48 |
|
46 |
| - |
| 49 | +# Add 3d scatter plot |
47 | 50 | ax = fig.add_subplot(251, projection='3d')
|
48 | 51 | ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=plt.cm.Spectral)
|
49 | 52 | ax.view_init(4, -72)
|
50 | 53 |
|
51 |
| -methods = ['standard', 'ltsa', 'hessian', 'modified'] |
52 |
| -labels = ['LLE', 'LTSA', 'Hessian LLE', 'Modified LLE'] |
53 |
| - |
54 |
| -for i, method in enumerate(methods): |
| 54 | +# Set-up manifold methods |
| 55 | +LLE = partial(manifold.LocallyLinearEmbedding, |
| 56 | + n_neighbors, n_components, eigen_solver='auto') |
| 57 | + |
| 58 | +methods = OrderedDict() |
| 59 | +methods['LLE'] = LLE(method='standard') |
| 60 | +methods['LTSA'] = LLE(method='ltsa') |
| 61 | +methods['Hessian LLE'] = LLE(method='hessian') |
| 62 | +methods['Modified LLE'] = LLE(method='modified') |
| 63 | +methods['Isomap'] = manifold.Isomap(n_neighbors, n_components) |
| 64 | +methods['MDS'] = manifold.MDS(n_components, max_iter=100, n_init=1) |
| 65 | +methods['SE'] = manifold.SpectralEmbedding(n_components=n_components, |
| 66 | + n_neighbors=n_neighbors) |
| 67 | +methods['t-SNE'] = manifold.TSNE(n_components=n_components, init='pca', |
| 68 | + random_state=0) |
| 69 | + |
| 70 | +# Plot results |
| 71 | +for i, (label, method) in enumerate(methods.items()): |
55 | 72 | t0 = time()
|
56 |
| - Y = manifold.LocallyLinearEmbedding(n_neighbors, n_components, |
57 |
| - eigen_solver='auto', |
58 |
| - method=method).fit_transform(X) |
| 73 | + Y = method.fit_transform(X) |
59 | 74 | t1 = time()
|
60 |
| - print("%s: %.2g sec" % (methods[i], t1 - t0)) |
61 |
| - |
62 |
| - ax = fig.add_subplot(252 + i) |
63 |
| - plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral) |
64 |
| - plt.title("%s (%.2g sec)" % (labels[i], t1 - t0)) |
| 75 | + print("%s: %.2g sec" % (label, t1 - t0)) |
| 76 | + ax = fig.add_subplot(2, 5, 2 + i + (i > 3)) |
| 77 | + ax.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral) |
| 78 | + ax.set_title("%s (%.2g sec)" % (label, t1 - t0)) |
65 | 79 | ax.xaxis.set_major_formatter(NullFormatter())
|
66 | 80 | ax.yaxis.set_major_formatter(NullFormatter())
|
67 |
| - plt.axis('tight') |
68 |
| - |
69 | <
8000
/code> | -t0 = time() |
70 |
| -Y = manifold.Isomap(n_neighbors, n_components).fit_transform(X) |
71 |
| -t1 = time() |
72 |
| -print("Isomap: %.2g sec" % (t1 - t0)) |
73 |
| -ax = fig.add_subplot(257) |
74 |
| -plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral) |
75 |
| -plt.title("Isomap (%.2g sec)" % (t1 - t0)) |
76 |
| -ax.xaxis.set_major_formatter(NullFormatter()) |
77 |
| -ax.yaxis.set_major_formatter(NullFormatter()) |
78 |
| -plt.axis('tight') |
79 |
| - |
80 |
| - |
81 |
| -t0 = time() |
82 |
| -mds = manifold.MDS(n_components, max_iter=100, n_init=1) |
83 |
| -Y = mds.fit_transform(X) |
84 |
| -t1 = time() |
85 |
| -print("MDS: %.2g sec" % (t1 - t0)) |
86 |
| -ax = fig.add_subplot(258) |
87 |
| -plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral) |
88 |
| -plt.title("MDS (%.2g sec)" % (t1 - t0)) |
89 |
| -ax.xaxis.set_major_formatter(NullFormatter()) |
90 |
| -ax.yaxis.set_major_formatter(NullFormatter()) |
91 |
| -plt.axis('tight') |
92 |
| - |
93 |
| - |
94 |
| -t0 = time() |
95 |
| -se = manifold.SpectralEmbedding(n_components=n_components, |
96 |
| - n_neighbors=n_neighbors) |
97 |
| -Y = se.fit_transform(X) |
98 |
| -t1 = time() |
99 |
| -print("SpectralEmbedding: %.2g sec" % (t1 - t0)) |
100 |
| -ax = fig.add_subplot(259) |
101 |
| -plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral) |
102 |
| -plt.title("SpectralEmbedding (%.2g sec)" % (t1 - t0)) |
103 |
| -ax.xaxis.set_major_formatter(NullFormatter()) |
104 |
| -ax.yaxis.set_major_formatter(NullFormatter()) |
105 |
| -plt.axis('tight') |
106 |
| - |
107 |
| -t0 = time() |
108 |
| -tsne = manifold.TSNE(n_components=n_components, init='pca', random_state=0) |
109 |
| -Y = tsne.fit_transform(X) |
110 |
| -t1 = time() |
111 |
| -print("t-SNE: %.2g sec" % (t1 - t0)) |
112 |
| -ax = fig.add_subplot(2, 5, 10) |
113 |
| -plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral) |
114 |
| -plt.title("t-SNE (%.2g sec)" % (t1 - t0)) |
115 |
| -ax.xaxis.set_major_formatter(NullFormatter()) |
116 |
| -ax.yaxis.set_major_formatter(NullFormatter()) |
117 |
| -plt.axis('tight') |
| 81 | + ax.axis('tight') |
118 | 82 |
|
119 | 83 | plt.show()
|
0 commit comments