Commit
·
9019bb1
1
Parent(s):
08dee09
Add application
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from sklearn.datasets import make_blobs
|
3 |
+
from sklearn.cluster import BisectingKMeans, KMeans
|
4 |
+
from functools import partial
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
|
7 |
+
|
8 |
+
def train_models(n_samples, n_clusters, cls_name):
|
9 |
+
default_base = {"n_samples": 500, "n_clusters": 1}
|
10 |
+
|
11 |
+
# Algorithms to compare
|
12 |
+
params = default_base.copy()
|
13 |
+
params.update({"n_samples":n_samples})
|
14 |
+
params.update({"n_clusters":n_clusters})
|
15 |
+
|
16 |
+
clustering_algorithms = {
|
17 |
+
"Bisecting K-Means": BisectingKMeans,
|
18 |
+
"K-Means": KMeans,
|
19 |
+
}
|
20 |
+
|
21 |
+
X, _ = make_blobs(n_samples=params["n_samples"], centers=2, random_state=0)
|
22 |
+
fig, ax = plt.subplots()
|
23 |
+
|
24 |
+
model = clustering_algorithms[cls_name]
|
25 |
+
algo = model(n_clusters=params["n_clusters"], random_state=0, n_init=3)
|
26 |
+
algo.fit(X)
|
27 |
+
centers = algo.cluster_centers_
|
28 |
+
|
29 |
+
ax.scatter(X[:, 0], X[:, 1], s=10, c=algo.labels_)
|
30 |
+
ax.scatter(centers[:, 0], centers[:, 1], c="r", s=20)
|
31 |
+
|
32 |
+
ax.set_title(f"{cls_name} : {params['n_clusters']} clusters")
|
33 |
+
|
34 |
+
# Hide x labels and tick labels for top plots and y ticks for right plots.
|
35 |
+
ax.label_outer()
|
36 |
+
ax.set_xticks([])
|
37 |
+
ax.set_yticks([])
|
38 |
+
return fig
|
39 |
+
|
40 |
+
|
41 |
+
def iter_grid(n_rows, n_cols):
|
42 |
+
# create a grid using gradio Block
|
43 |
+
for _ in range(n_rows):
|
44 |
+
with gr.Row():
|
45 |
+
for _ in range(n_cols):
|
46 |
+
with gr.Column():
|
47 |
+
yield
|
48 |
+
|
49 |
+
|
50 |
+
title = "📊 Performance Comparison: Bisecting vs Regular K-Means"
|
51 |
+
with gr.Blocks(title=title) as demo:
|
52 |
+
gr.Markdown(f"## {title}")
|
53 |
+
gr.Markdown("This example shows differences between "
|
54 |
+
+ "Regular K-Means algorithm and Bisecting K-Means. ")
|
55 |
+
|
56 |
+
|
57 |
+
input_models = ["Bisecting K-Means", "K-Means"]
|
58 |
+
|
59 |
+
n_samples = gr.Slider(minimum=500, maximum=2000, step=50,
|
60 |
+
label = "Number of Samples")
|
61 |
+
|
62 |
+
n_clusters = gr.Slider(minimum=1, maximum=20, step=1,
|
63 |
+
label = "Number of Clusters")
|
64 |
+
counter = 0
|
65 |
+
|
66 |
+
for _ in iter_grid(1,2):
|
67 |
+
if counter >= len(input_models):
|
68 |
+
break
|
69 |
+
|
70 |
+
input_model = input_models[counter]
|
71 |
+
plot = gr.Plot(label=input_model)
|
72 |
+
|
73 |
+
fn = partial(train_models, cls_name=input_model)
|
74 |
+
n_samples.change(fn=fn, inputs=[n_samples, n_clusters], outputs=plot)
|
75 |
+
|
76 |
+
n_clusters.change(fn=fn, inputs=[n_samples, n_clusters], outputs=plot)
|
77 |
+
counter += 1
|
78 |
+
|
79 |
+
demo.launch()
|