caliex commited on
Commit
2ed0ebe
·
1 Parent(s): 59f73be

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import matplotlib.pyplot as plt
3
+ from matplotlib import ticker
4
+ from sklearn import manifold, datasets
5
+ from mpl_toolkits.mplot3d import Axes3D
6
+
7
+
8
+ def compare_manifold_learning(methods, n_samples, n_neighbors, n_components, perplexity):
9
+ S_points, S_color = datasets.make_s_curve(n_samples, random_state=0)
10
+ transformed_data = []
11
+
12
+ if len(methods) == 1:
13
+ method = methods[0]
14
+ manifold_method = {
15
+ "Locally Linear Embeddings Standard": manifold.LocallyLinearEmbedding(method="standard", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
16
+ "Locally Linear Embeddings LTSA": manifold.LocallyLinearEmbedding(method="ltsa", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
17
+ "Locally Linear Embeddings Hessian": manifold.LocallyLinearEmbedding(method="hessian", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
18
+ "Locally Linear Embeddings Modified": manifold.LocallyLinearEmbedding(method="modified", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
19
+ "Isomap": manifold.Isomap(n_neighbors=n_neighbors, n_components=n_components, p=1),
20
+ "MultiDimensional Scaling": manifold.MDS(n_components=n_components, max_iter=50, n_init=4, random_state=0, normalized_stress=False),
21
+ "Spectral Embedding": manifold.SpectralEmbedding(n_components=n_components, n_neighbors=n_neighbors),
22
+ "T-distributed Stochastic Neighbor Embedding": manifold.TSNE(n_components=n_components, perplexity=perplexity, init="random", n_iter=250, random_state=0)
23
+ }[method]
24
+ S_transformed = manifold_method.fit_transform(S_points)
25
+ transformed_data.append(S_transformed)
26
+ else:
27
+ for method in methods:
28
+ manifold_method = {
29
+ "Locally Linear Embeddings Standard": manifold.LocallyLinearEmbedding(method="standard", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
30
+ "Locally Linear Embeddings LTSA": manifold.LocallyLinearEmbedding(method="ltsa", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
31
+ "Locally Linear Embeddings Hessian": manifold.LocallyLinearEmbedding(method="hessian", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
32
+ "Locally Linear Embeddings Modified": manifold.LocallyLinearEmbedding(method="modified", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
33
+ "Isomap": manifold.Isomap(n_neighbors=n_neighbors, n_components=n_components, p=1),
34
+ "MultiDimensional Scaling": manifold.MDS(n_components=n_components, max_iter=50, n_init=4, random_state=0, normalized_stress=False),
35
+ "Spectral Embedding": manifold.SpectralEmbedding(n_components=n_components, n_neighbors=n_neighbors),
36
+ "T-distributed Stochastic Neighbor Embedding": manifold.TSNE(n_components=n_components, perplexity=perplexity, init="random", n_iter=250, random_state=0)
37
+ }[method]
38
+ S_transformed = manifold_method.fit_transform(S_points)
39
+ transformed_data.append(S_transformed)
40
+
41
+ fig, axs = plt.subplots(1, len(transformed_data), figsize=(6 * len(transformed_data), 6))
42
+ fig.suptitle("Manifold Learning Comparison", fontsize=16)
43
+
44
+ if len(methods) == 1:
45
+ ax = axs
46
+ method = methods[0]
47
+ data = transformed_data[0]
48
+ ax.scatter(data[:, 0], data[:, 1], c=S_color, cmap=plt.cm.Spectral)
49
+ ax.set_title(f"Method: {method}")
50
+ ax.axis("tight")
51
+ ax.axis("off")
52
+ ax.xaxis.set_major_locator(ticker.NullLocator())
53
+ ax.yaxis.set_major_locator(ticker.NullLocator())
54
+ else:
55
+ for ax, method, data in zip(axs, methods, transformed_data):
56
+ ax.scatter(data[:, 0], data[:, 1], c=S_color, cmap=plt.cm.Spectral)
57
+ ax.set_title(f"Method: {method}")
58
+ ax.axis("tight")
59
+ ax.axis("off")
60
+ ax.xaxis.set_major_locator(ticker.NullLocator())
61
+ ax.yaxis.set_major_locator(ticker.NullLocator())
62
+
63
+ plt.tight_layout()
64
+ plt.savefig("plot.png")
65
+ plt.close()
66
+
67
+ return "plot.png"
68
+
69
+ method_options = [
70
+ "Locally Linear Embeddings Standard",
71
+ "Locally Linear Embeddings LTSA",
72
+ "Locally Linear Embeddings Hessian",
73
+ "Locally Linear Embeddings Modified",
74
+ "Isomap",
75
+ "MultiDimensional Scaling",
76
+ "Spectral Embedding",
77
+ "T-distributed Stochastic Neighbor Embedding"
78
+ ]
79
+
80
+ inputs = [
81
+ gr.components.CheckboxGroup(method_options, label="Manifold Learning Methods"),
82
+ gr.inputs.Slider(default=1500, label="Number of Samples", maximum=5000),
83
+ gr.inputs.Slider(default=12, label="Number of Neighbors"),
84
+ gr.inputs.Slider(default=2, label="Number of Components"),
85
+ gr.inputs.Slider(default=30, label="Perplexity (for t-SNE)")
86
+ ]
87
+
88
+ gr.Interface(
89
+ fn=compare_manifold_learning,
90
+ inputs=inputs,
91
+ outputs="image",
92
+ examples=[
93
+ [method_options, 1500, 12, 2, 30]
94
+ ],
95
+ title="Manifold Learning Comparison",
96
+ description="This code demonstrates a comparison of manifold learning methods using the S-curve dataset. Manifold learning techniques aim to uncover the underlying structure and relationships within high-dimensional data by projecting it onto a lower-dimensional space. This comparison allows you to explore the effects of different methods on the dataset. See the original scikit-learn example here: https://scikit-learn.org/stable/auto_examples/manifold/plot_compare_methods.html"
97
+ ).launch()