caliex commited on
Commit
808868e
·
1 Parent(s): 63caa40

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ for method in methods:
13
+ manifold_method = {
14
+ "LLE Standard": manifold.LocallyLinearEmbedding(method="standard", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
15
+ "LLE LTSA": manifold.LocallyLinearEmbedding(method="ltsa", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
16
+ "LLE Hessian": manifold.LocallyLinearEmbedding(method="hessian", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
17
+ "LLE Modified": manifold.LocallyLinearEmbedding(method="modified", n_neighbors=n_neighbors, n_components=n_components, eigen_solver="auto", random_state=0),
18
+ "Isomap": manifold.Isomap(n_neighbors=n_neighbors, n_components=n_components, p=1),
19
+ "MDS": manifold.MDS(n_components=n_components, max_iter=50, n_init=4, random_state=0, normalized_stress=False),
20
+ "Spectral Embedding": manifold.SpectralEmbedding(n_components=n_components, n_neighbors=n_neighbors),
21
+ "t-SNE": manifold.TSNE(n_components=n_components, perplexity=perplexity, init="random", n_iter=250, random_state=0)
22
+ }[method]
23
+ S_transformed = manifold_method.fit_transform(S_points)
24
+ transformed_data.append(S_transformed)
25
+
26
+ fig, axs = plt.subplots(1, len(transformed_data), figsize=(6 * len(transformed_data), 6))
27
+ fig.suptitle("Manifold Learning Comparison", fontsize=16)
28
+
29
+ for ax, method, data in zip(axs, methods, transformed_data):
30
+ ax.scatter(data[:, 0], data[:, 1], c=S_color, cmap=plt.cm.Spectral)
31
+ ax.set_title(f"Method: {method}")
32
+ ax.axis("tight")
33
+ ax.axis("off")
34
+ ax.xaxis.set_major_locator(ticker.NullLocator())
35
+ ax.yaxis.set_major_locator(ticker.NullLocator())
36
+
37
+ plt.tight_layout()
38
+ plt.savefig("plot.png")
39
+ plt.close()
40
+
41
+ return "plot.png"
42
+
43
+ method_options = [
44
+ "LLE Standard",
45
+ "LLE LTSA",
46
+ "LLE Hessian",
47
+ "LLE Modified",
48
+ "Isomap",
49
+ "MDS",
50
+ "Spectral Embedding",
51
+ "t-SNE"
52
+ ]
53
+
54
+ inputs = [
55
+ gr.components.CheckboxGroup(method_options, label="Manifold Learning Methods"),
56
+ gr.inputs.Slider(default=1500, label="Number of Samples", maximum=5000),
57
+ gr.inputs.Slider(default=12, label="Number of Neighbors"),
58
+ gr.inputs.Slider(default=2, label="Number of Components"),
59
+ gr.inputs.Slider(default=30, label="Perplexity (for t-SNE)")
60
+ ]
61
+
62
+ gr.Interface(
63
+ fn=compare_manifold_learning,
64
+ inputs=inputs,
65
+ outputs="image",
66
+ examples=[
67
+ [method_options, 1500, 12, 2, 30]
68
+ ],
69
+ title="Manifold Learning Comparison",
70
+ description="Compare manifold learning methods on the S-curve dataset."
71
+ ).launch()