Created gradio implementation of the example.
Browse files- app.py +62 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gradio Implementation: Lenix Carter
|
| 2 |
+
# License: BSD 3-Clause or CC-0
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
from sklearn.cluster import kmeans_plusplus
|
| 8 |
+
from sklearn.datasets import make_blobs
|
| 9 |
+
|
| 10 |
+
plt.switch_backend("agg")
|
| 11 |
+
|
| 12 |
+
def initial_points(n_samples, n_components, clst_std, n_clust):
|
| 13 |
+
plt.clf()
|
| 14 |
+
# Generate sample data
|
| 15 |
+
|
| 16 |
+
X, y_true = make_blobs(
|
| 17 |
+
n_samples=n_samples, centers=n_components, cluster_std=clst_std, random_state=0
|
| 18 |
+
)
|
| 19 |
+
X = X[:, ::-1]
|
| 20 |
+
|
| 21 |
+
# Calculate seeds from k-means++
|
| 22 |
+
centers_init, indices = kmeans_plusplus(X, n_clusters=n_clust, random_state=0)
|
| 23 |
+
|
| 24 |
+
# Plot init seeds along side sample data
|
| 25 |
+
plt.figure(1)
|
| 26 |
+
|
| 27 |
+
for k in range(n_components):
|
| 28 |
+
cluster_data = y_true == k
|
| 29 |
+
plt.scatter(X[cluster_data, 0], X[cluster_data, 1], marker=".", s=10)
|
| 30 |
+
|
| 31 |
+
plt.scatter(centers_init[:, 0], centers_init[:, 1], c="b", s=50)
|
| 32 |
+
plt.title("K-Means++ Initialization")
|
| 33 |
+
plt.xticks([])
|
| 34 |
+
plt.yticks([])
|
| 35 |
+
return plt
|
| 36 |
+
|
| 37 |
+
title = "An example of K-Means++ Initialization"
|
| 38 |
+
with gr.Blocks() as demo:
|
| 39 |
+
gr.Markdown(f" # {title}")
|
| 40 |
+
gr.Markdown("""
|
| 41 |
+
This example shows the ouput of the K-Means++ function.
|
| 42 |
+
|
| 43 |
+
This is based on the example [here](https://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_plusplus.html#sphx-glr-auto-examples-cluster-plot-kmeans-plusplus-py).
|
| 44 |
+
""")
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column():
|
| 47 |
+
n_samples = gr.Slider(100, 4000, 1000, label="Number of Samples")
|
| 48 |
+
n_components = gr.Slider(1, 10, 4, step=1, label="Number of blobs")
|
| 49 |
+
clst_std = gr.Slider(.1, 1, .6, label="Blob Standard Deviation")
|
| 50 |
+
n_clusters = gr.Slider(1, 10, 4, step=1, label="Number of Clusters to Initialize")
|
| 51 |
+
btn = gr.Button(label="Run")
|
| 52 |
+
with gr.Column():
|
| 53 |
+
graph_points = gr.Plot(label="K-Means++ Initial Points")
|
| 54 |
+
btn.click(
|
| 55 |
+
fn=initial_points,
|
| 56 |
+
inputs=[n_samples, n_components, clst_std, n_clusters],
|
| 57 |
+
outputs=[graph_points]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if __name__ == '__main__':
|
| 61 |
+
demo.launch()
|
| 62 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
matplotlib==3.6.3
|
| 2 |
+
scikit-learn==1.2.2
|