File size: 5,348 Bytes
75df68a d87080c 75df68a d87080c 75df68a d87080c 75df68a |
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
import time
import warnings
from functools import partial
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
from sklearn import cluster, datasets
from sklearn.preprocessing import StandardScaler
from itertools import cycle, islice
def train_models(selected_data, n_samples, n_clusters, n_neighbors, cls_name):
np.random.seed(0)
default_base = {"n_neighbors": 10, "n_clusters": 3}
noisy_circles = datasets.make_circles(n_samples=n_samples, factor=0.5, noise=0.05)
noisy_moons = datasets.make_moons(n_samples=n_samples, noise=0.05)
blobs = datasets.make_blobs(n_samples=n_samples, random_state=8)
no_structure = np.random.rand(n_samples, 2), None
# Anisotropicly distributed data
random_state = 170
X, y = datasets.make_blobs(n_samples=n_samples, random_state=random_state)
transformation = [[0.6, -0.6], [-0.4, 0.8]]
X_aniso = np.dot(X, transformation)
aniso = (X_aniso, y)
# blobs with varied variances
varied = datasets.make_blobs(
n_samples=n_samples, cluster_std=[1.0, 2.5, 0.5], random_state=random_state
)
dataset_list = {
"Noisy Circles": [noisy_circles, {"n_clusters": n_clusters}],
"Noisy Moons": [noisy_moons, {"n_clusters": n_clusters}],
"Varied": [varied, {"n_neighbors": n_neighbors}],
"Aniso": [aniso, {"n_neighbors": n_neighbors}],
"Blobs": [blobs, {}],
"No Structure": [no_structure, {}],
}
params = default_base.copy()
params.update(dataset_list[selected_data][1])
X, y = dataset_list[selected_data][0]
X = StandardScaler().fit_transform(X)
ward = cluster.AgglomerativeClustering(
n_clusters=params["n_clusters"], linkage="ward"
)
complete = cluster.AgglomerativeClustering(
n_clusters=params["n_clusters"], linkage="complete"
)
average = cluster.AgglomerativeClustering(
n_clusters=params["n_clusters"], linkage="average"
)
single = cluster.AgglomerativeClustering(
n_clusters=params["n_clusters"], linkage="single"
)
clustering_algorithms = {
"Single Linkage": single,
"Average Linkage": average,
"Complete Linkage": complete,
"Ward Linkage": ward,
}
t0 = time.time()
algorithm = clustering_algorithms[cls_name]
# catch warnings related to kneighbors_graph
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="the number of connected components of the "
+ "connectivity matrix is [0-9]{1,2}"
+ " > 1. Completing it to avoid stopping the tree early.",
category=UserWarning,
)
algorithm.fit(X)
t1 = time.time()
if hasattr(algorithm, "labels_"):
y_pred = algorithm.labels_.astype(int)
else:
y_pred = algorithm.predict(X)
fig, ax = plt.subplots()
colors = np.array(
list(
islice(
cycle(
[
"#377eb8",
"#ff7f00",
"#4daf4a",
"#f781bf",
"#a65628",
"#984ea3",
"#999999",
"#e41a1c",
"#dede00",
]
),
int(max(y_pred) + 1),
)
)
)
ax.scatter(X[:, 0], X[:, 1], color=colors[y_pred])
ax.set_xlim(-2.5, 2.5)
ax.set_ylim(-2.5, 2.5)
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 = "π§π»βπ¬ Compare linkages in hierarchical clustering π§π»βπ¬"
with gr.Blocks(title=title) as demo:
gr.Markdown(f"## {title}")
gr.Markdown("This app demonstrates different linkage methods in"
" hierarchical clustering π")
input_models = ["Single Linkage", "Average Linkage", "Complete Linkage",
"Ward Linkage"]
input_data = gr.Radio(
choices=["Noisy Circles", "Noisy Moons",
"Varied", "Aniso", "Blobs", "No Structure"],
value="Noisy Moons"
)
n_samples = gr.Slider(minimum=500, maximum=2000, step=50,
label = "Number of Samples")
n_neighbors = gr.Slider(minimum=2, maximum=5, step=1,
label = "Number of neighbors")
n_clusters = gr.Slider(minimum=2, maximum=5, step=1,
label = "Number of Clusters")
counter = 0
for _ in iter_grid(2, 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)
input_data.change(fn=fn, inputs=[input_data, n_samples, n_clusters, n_neighbors], outputs=plot)
n_samples.change(fn=fn, inputs=[input_data, n_samples, n_clusters, n_neighbors], outputs=plot)
n_neighbors.change(fn=fn, inputs=[input_data, n_samples, n_clusters, n_neighbors], outputs=plot)
n_clusters.change(fn=fn, inputs=[input_data, n_samples, n_clusters, n_neighbors], outputs=plot)
counter += 1
demo.launch() |