initial commit
Browse files- app.py +50 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
|
4 |
+
from sklearn.cluster import AgglomerativeClustering
|
5 |
+
from sklearn.neighbors import kneighbors_graph
|
6 |
+
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
|
10 |
+
np.random.seed(42)
|
11 |
+
|
12 |
+
def agglomorative_cluster(n_samples: int, n_neighbours: int, n_clusters: int, linkage: str, connectivity: bool) -> "plt.Figure":
|
13 |
+
|
14 |
+
t = 1.5 * np.pi * (1 + 3 * np.random.rand(1, n_samples))
|
15 |
+
x = t * np.cos(t)
|
16 |
+
y = t * np.sin(t)
|
17 |
+
|
18 |
+
X = np.concatenate((x, y))
|
19 |
+
X += 0.7 * np.random.randn(2, n_samples)
|
20 |
+
X = X.T
|
21 |
+
|
22 |
+
knn_graph = kneighbors_graph(X, n_neighbors=n_neighbours, include_self=False)
|
23 |
+
connectivity = knn_graph if not connectivity else None
|
24 |
+
|
25 |
+
fig, ax = plt.subplots(1, 1, figsize=(24, 15))
|
26 |
+
model = AgglomerativeClustering(linkage=linkage, connectivity=connectivity, n_clusters=int(n_clusters))
|
27 |
+
model.fit(X)
|
28 |
+
ax.scatter(X[:, 0], X[:, 1], c=model.labels_, cmap=plt.cm.nipy_spectral)
|
29 |
+
ax.axis("equal")
|
30 |
+
ax.axis("off")
|
31 |
+
|
32 |
+
return fig
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
demo = gr.Interface(
|
39 |
+
fn = agglomorative_cluster,
|
40 |
+
inputs = [gr.Slider(0, 20_000, label="n_samples", info="the number of samples in the data.", step=1),
|
41 |
+
gr.Slider(0, 30, label="n_neighbours", info="the number of neighbours in the data", step=1),
|
42 |
+
gr.Dropdown([3, 30], label="n_clusters", info="the number of clusters in the data"),
|
43 |
+
gr.Dropdown(['average', 'complete', 'ward', 'single'], label="linkage", info="the different types of aggolomorative clustering techniques"),
|
44 |
+
gr.Checkbox(True, label="connectivity", info="whether to impose a connectivity into the graph")],
|
45 |
+
|
46 |
+
outputs = [gr.Plot(label="Plot")]
|
47 |
+
)
|
48 |
+
|
49 |
+
demo.launch()
|
50 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
scikit-learn
|
2 |
+
scipy
|
3 |
+
numpy
|
4 |
+
pandas
|
5 |
+
matplotlib
|