LenixC's picture
Created gradio implementation of the example.
95c86eb
raw
history blame
2.11 kB
# Gradio Implementation: Lenix Carter
# License: BSD 3-Clause or CC-0
import gradio as gr
import matplotlib.pyplot as plt
from sklearn.cluster import kmeans_plusplus
from sklearn.datasets import make_blobs
plt.switch_backend("agg")
def initial_points(n_samples, n_components, clst_std, n_clust):
plt.clf()
# Generate sample data
X, y_true = make_blobs(
n_samples=n_samples, centers=n_components, cluster_std=clst_std, random_state=0
)
X = X[:, ::-1]
# Calculate seeds from k-means++
centers_init, indices = kmeans_plusplus(X, n_clusters=n_clust, random_state=0)
# Plot init seeds along side sample data
plt.figure(1)
for k in range(n_components):
cluster_data = y_true == k
plt.scatter(X[cluster_data, 0], X[cluster_data, 1], marker=".", s=10)
plt.scatter(centers_init[:, 0], centers_init[:, 1], c="b", s=50)
plt.title("K-Means++ Initialization")
plt.xticks([])
plt.yticks([])
return plt
title = "An example of K-Means++ Initialization"
with gr.Blocks() as demo:
gr.Markdown(f" # {title}")
gr.Markdown("""
This example shows the ouput of the K-Means++ function.
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).
""")
with gr.Row():
with gr.Column():
n_samples = gr.Slider(100, 4000, 1000, label="Number of Samples")
n_components = gr.Slider(1, 10, 4, step=1, label="Number of blobs")
clst_std = gr.Slider(.1, 1, .6, label="Blob Standard Deviation")
n_clusters = gr.Slider(1, 10, 4, step=1, label="Number of Clusters to Initialize")
btn = gr.Button(label="Run")
with gr.Column():
graph_points = gr.Plot(label="K-Means++ Initial Points")
btn.click(
fn=initial_points,
inputs=[n_samples, n_components, clst_std, n_clusters],
outputs=[graph_points]
)
if __name__ == '__main__':
demo.launch()