File size: 2,113 Bytes
95c86eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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()