Hnabil commited on
Commit
1cc0557
·
1 Parent(s): a5feb84

Add Application file

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from time import time
2
+ import gradio as gr
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import plotly.graph_objects as go
6
+
7
+ from sklearn import manifold, datasets
8
+ from sklearn.cluster import AgglomerativeClustering
9
+
10
+
11
+ SEED = 0
12
+ digits = datasets.load_digits()
13
+ X, y = digits.data, digits.target
14
+ n_samples, n_features = X.shape
15
+ np.random.seed(SEED)
16
+
17
+ import matplotlib
18
+ matplotlib.use('Agg')
19
+
20
+
21
+
22
+ def plot_clustering(linkage, dim):
23
+ if dim == '3D':
24
+ X_red = manifold.SpectralEmbedding(n_components=3).fit_transform(X)
25
+ else:
26
+ X_red = manifold.SpectralEmbedding(n_components=2).fit_transform(X)
27
+
28
+ clustering = AgglomerativeClustering(linkage=linkage, n_clusters=10)
29
+
30
+ t0 = time()
31
+ clustering.fit(X_red)
32
+ print("%s :\t%.2fs" % (linkage, time() - t0))
33
+
34
+ labels = clustering.labels_
35
+
36
+ x_min, x_max = np.min(X_red, axis=0), np.max(X_red, axis=0)
37
+ X_red = (X_red - x_min) / (x_max - x_min)
38
+
39
+ fig = go.Figure()
40
+
41
+ for digit in digits.target_names:
42
+ subset = X_red[y==digit]
43
+ rgbas = plt.cm.nipy_spectral(labels[y == digit]/10)
44
+ color = [f'rgba({rgba[0]}, {rgba[1]}, {rgba[2]}, 0.8)' for rgba in rgbas]
45
+ if dim == '2D':
46
+ fig.add_trace(go.Scatter(x=subset[:,0], y=subset[:,1], mode='text', text=str(digit), textfont={'size': 16, 'color': color}))
47
+ elif dim == '3D':
48
+ fig.add_trace(go.Scatter3d(x=subset[:,0], y=subset[:,1], z=subset[:,2], mode='text', text=str(digit), textfont={'size': 16, 'color': color}))
49
+
50
+ fig.update_traces(showlegend=False)
51
+
52
+ return fig
53
+
54
+
55
+ title = '# Agglomerative Clustering on MNIST'
56
+
57
+ description = """
58
+ An illustration of various linkage option for [agglomerative clustering](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.AgglomerativeClustering.html) on the digits dataset.
59
+ """
60
+
61
+ author = '''
62
+ Created by [@Hnabil](https://huggingface.co/Hnabil) based on [scikit-learn docs](https://scikit-learn.org/stable/auto_examples/cluster/plot_digits_linkage.html)
63
+ '''
64
+
65
+ with gr.Blocks(analytics_enabled=False, title=title) as demo:
66
+ gr.Markdown(title)
67
+ gr.Markdown(description)
68
+ gr.Markdown(author)
69
+
70
+ with gr.Row():
71
+ with gr.Column():
72
+ linkage = gr.Radio(["ward", "average", "complete", "single"], value="average", interactive=True, label="Linkage Method")
73
+ dim = gr.Radio(['2D', '3D'], label='Embedding Dimensionality', value='2D')
74
+
75
+ btn = gr.Button('Submit')
76
+
77
+ with gr.Column():
78
+ plot = gr.Plot(label='MNIST Embeddings')
79
+
80
+ btn.click(plot_clustering, inputs=[linkage, dim], outputs=[plot])
81
+ demo.load(plot_clustering, inputs=[linkage, dim], outputs=[plot])
82
+
83
+ demo.launch()