File size: 2,352 Bytes
9019bb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
from sklearn.datasets import make_blobs
from sklearn.cluster import BisectingKMeans, KMeans
from functools import partial
import matplotlib.pyplot as plt


def train_models(n_samples, n_clusters, cls_name):
    default_base = {"n_samples": 500, "n_clusters": 1}

    # Algorithms to compare
    params = default_base.copy()
    params.update({"n_samples":n_samples})
    params.update({"n_clusters":n_clusters})
    
    clustering_algorithms = {
    "Bisecting K-Means": BisectingKMeans,
    "K-Means": KMeans,
    }

    X, _ = make_blobs(n_samples=params["n_samples"], centers=2, random_state=0)
    fig, ax = plt.subplots()

    model = clustering_algorithms[cls_name]
    algo = model(n_clusters=params["n_clusters"], random_state=0, n_init=3)
    algo.fit(X)
    centers = algo.cluster_centers_

    ax.scatter(X[:, 0], X[:, 1], s=10, c=algo.labels_)
    ax.scatter(centers[:, 0], centers[:, 1], c="r", s=20)

    ax.set_title(f"{cls_name} : {params['n_clusters']} clusters")

    # Hide x labels and tick labels for top plots and y ticks for right plots.
    ax.label_outer()
    ax.set_xticks([])
    ax.set_yticks([])
    return fig


def iter_grid(n_rows, n_cols):
    # create a grid using gradio Block
    for _ in range(n_rows):
        with gr.Row():
            for _ in range(n_cols):
                with gr.Column():
                    yield


title = "📊 Performance Comparison: Bisecting vs Regular K-Means"
with gr.Blocks(title=title) as demo:
    gr.Markdown(f"## {title}")
    gr.Markdown("This example shows differences between "
                + "Regular K-Means algorithm and Bisecting K-Means. ")


    input_models = ["Bisecting K-Means", "K-Means"]
    
    n_samples = gr.Slider(minimum=500, maximum=2000, step=50, 
    label = "Number of Samples")
    
    n_clusters = gr.Slider(minimum=1, maximum=20, step=1, 
    label = "Number of Clusters")
    counter = 0

    for _ in iter_grid(1,2):
        if counter >= len(input_models):
            break

        input_model = input_models[counter]
        plot = gr.Plot(label=input_model)

        fn = partial(train_models, cls_name=input_model)
        n_samples.change(fn=fn, inputs=[n_samples, n_clusters], outputs=plot)
        
        n_clusters.change(fn=fn, inputs=[n_samples, n_clusters], outputs=plot)
        counter += 1

demo.launch()