Created the demo
Browse files- app.py +79 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from sklearn.cluster import AffinityPropagation
|
| 4 |
+
from sklearn import metrics
|
| 5 |
+
from sklearn.datasets import make_blobs
|
| 6 |
+
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import matplotlib
|
| 9 |
+
matplotlib.use('agg')
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
def generate_data(num_centers, num_samples):
|
| 14 |
+
all_centers = [[1, 1], [-1, -1], [1, -1], [-1, 1]]
|
| 15 |
+
centers = all_centers[:num_centers]
|
| 16 |
+
|
| 17 |
+
X, labels_true = make_blobs(n_samples=num_samples, centers=centers, cluster_std=0.5, random_state=0)
|
| 18 |
+
|
| 19 |
+
return X, labels_true
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def create_plot(num_clusters, num_samples):
|
| 23 |
+
X, labels_true = generate_data(num_clusters, num_samples)
|
| 24 |
+
|
| 25 |
+
af = AffinityPropagation(preference=-50, random_state=0).fit(X)
|
| 26 |
+
cluster_centers_indices = af.cluster_centers_indices_
|
| 27 |
+
labels = af.labels_
|
| 28 |
+
|
| 29 |
+
n_clusters_ = len(cluster_centers_indices)
|
| 30 |
+
|
| 31 |
+
metrics_str = f"Estimated number of clusters: {n_clusters_}\n"
|
| 32 |
+
metrics_str += f"Homogeneity: {metrics.homogeneity_score(labels_true, labels):0.3f}\n"
|
| 33 |
+
metrics_str += f"Completeness: {metrics.completeness_score(labels_true, labels):0.3f}\n"
|
| 34 |
+
metrics_str += f"V-measure: {metrics.v_measure_score(labels_true, labels):0.3f}\n"
|
| 35 |
+
metrics_str += f"Adjusted Rand Index: {metrics.adjusted_rand_score(labels_true, labels):0.3f}\n"
|
| 36 |
+
metrics_str += f"Adjusted Mutual Information: {metrics.adjusted_mutual_info_score(labels_true, labels):0.3f}\n"
|
| 37 |
+
metrics_str += f"Silhouette Coefficient: {metrics.silhouette_score(X, labels, metric='sqeuclidean'):0.3f}\n"
|
| 38 |
+
|
| 39 |
+
fig = plt.figure(1)
|
| 40 |
+
plt.clf()
|
| 41 |
+
|
| 42 |
+
colors = plt.cycler("color", plt.cm.viridis(np.linspace(0, 1, n_clusters_)))
|
| 43 |
+
|
| 44 |
+
for k, col in zip(range(n_clusters_), colors):
|
| 45 |
+
class_members = labels == k
|
| 46 |
+
cluster_center = X[cluster_centers_indices[k]]
|
| 47 |
+
plt.scatter(
|
| 48 |
+
X[class_members, 0], X[class_members, 1], color=col["color"], marker="."
|
| 49 |
+
)
|
| 50 |
+
plt.scatter(
|
| 51 |
+
cluster_center[0], cluster_center[1], s=14, color=col["color"], marker="o"
|
| 52 |
+
)
|
| 53 |
+
for x in X[class_members]:
|
| 54 |
+
plt.plot(
|
| 55 |
+
[cluster_center[0], x[0]], [cluster_center[1], x[1]], color=col["color"]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
plt.title("Estimated number of clusters: %d" % n_clusters_)
|
| 59 |
+
|
| 60 |
+
return fig, metrics_str
|
| 61 |
+
|
| 62 |
+
title = "Affinity propagation clustering algorithm"
|
| 63 |
+
description = "This demo plots clusters of a synthetic 2D dataset that contains up to 4 clusters using the affinity propagation algorithm."
|
| 64 |
+
with gr.Blocks() as demo:
|
| 65 |
+
gr.Markdown(f"## {title}")
|
| 66 |
+
gr.Markdown(description)
|
| 67 |
+
|
| 68 |
+
num_clusters = gr.Slider(minimum=2, maximum=4, step=1, value=2, label="Number of clusters")
|
| 69 |
+
num_samples = gr.Slider(minimum=100, maximum=300, step=100, value=200, label="Number of samples")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
plot = gr.Plot()
|
| 73 |
+
text_box = gr.Textbox(label="Results")
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
num_clusters.change(fn=create_plot, inputs=[num_clusters, num_samples], outputs=[plot, text_box])
|
| 77 |
+
num_samples.change(fn=create_plot, inputs=[num_clusters, num_samples], outputs=[plot, text_box])
|
| 78 |
+
|
| 79 |
+
demo.launch(enable_queue=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scikit-learn
|
| 2 |
+
matplotlib
|
| 3 |
+
numpy
|
| 4 |
+
|