File size: 3,489 Bytes
ce24de9 bab8a75 ce24de9 |
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 |
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import AgglomerativeClustering
from sklearn.neighbors import kneighbors_graph
import gradio as gr
np.random.seed(42)
def agglomorative_cluster(n_samples: int, n_neighbours: int, n_clusters: int, linkage: str, connectivity: bool) -> "plt.Figure":
t = 1.5 * np.pi * (1 + 3 * np.random.rand(1, n_samples))
x = t * np.cos(t)
y = t * np.sin(t)
X = np.concatenate((x, y))
X += 0.7 * np.random.randn(2, n_samples)
X = X.T
knn_graph = kneighbors_graph(X, n_neighbors=n_neighbours, include_self=False)
connectivity = knn_graph if not connectivity else None
fig, ax = plt.subplots(1, 1, figsize=(24, 15))
model = AgglomerativeClustering(linkage=linkage, connectivity=connectivity, n_clusters=int(n_clusters))
model.fit(X)
ax.scatter(X[:, 0], X[:, 1], c=model.labels_, cmap=plt.cm.nipy_spectral)
ax.axis("equal")
ax.axis("off")
return fig
with gr.Blocks() as demo:
gr.Markdown("""
# Agglomorative Clustering with and without Structure
This space is an implementation of the scikit-learn document [Agglomorative clustering with and without structure](https://scikit-learn.org/stable/auto_examples/cluster/plot_agglomerative_clustering.html#sphx-glr-auto-examples-cluster-plot-agglomerative-clustering-py)
This space shows the effects of imposing **connectivity graph** to capture local structure in the data.
You can uncheck the checkbox `connectivity` to see the effect on data clustering without **connectivity graph**. There are other parameters in this space
which you can play with such as `n_samples` (the number of data samples), `n_neighbours` (the number of neighbours), `n_clusters` (the number of clusters) and
what type of linkage to use for Agglomorative clustering `linkage`.
Have fun playing with the tool 🤗
""")
n_samples = gr.Slider(0, 20_000, label="n_samples", info="the number of samples in the data.", step=1)
n_neighbours = gr.Slider(0, 30, label="n_neighbours", info="the number of neighbours in the data", step=1)
n_clusters = gr.Slider(3, 30, label="n_clusters", info="the number of clusters in the data", step=2)
linkage = gr.Dropdown(['average', 'complete', 'ward', 'single'], label="linkage", info="the different types of aggolomorative clustering techniques")
connectivity = gr.Checkbox(True, label="connectivity", info="whether to impose a connectivity into the graph")
output = gr.Plot(label="Plot")
plot_btn = gr.Button("Plot")
plot_btn.click(fn=agglomorative_cluster, inputs=[n_samples, n_neighbours, n_clusters, linkage, connectivity],
outputs=output, api_name="plotcluster")
# demo = gr.Interface(
# fn = agglomorative_cluster,
# inputs = [gr.Slider(0, 20_000, label="n_samples", info="the number of samples in the data.", step=1),
# gr.Slider(0, 30, label="n_neighbours", info="the number of neighbours in the data", step=1),
# gr.Dropdown([3, 30], label="n_clusters", info="the number of clusters in the data"),
# gr.Dropdown(['average', 'complete', 'ward', 'single'], label="linkage", info="the different types of aggolomorative clustering techniques"),
# gr.Checkbox(True, label="connectivity", info="whether to impose a connectivity into the graph")],
# outputs = [gr.Plot(label="Plot")]
# )
demo.launch()
|