Ben Burtenshaw commited on
Commit
e318fce
β€’
1 Parent(s): 66d0cd2

formatting

Browse files
Files changed (1) hide show
  1. app.py +36 -21
app.py CHANGED
@@ -8,18 +8,17 @@ import matplotlib.cm as cm
8
  import numpy as np
9
 
10
  theme = gr.themes.Monochrome(
11
- primary_hue="indigo",
12
- secondary_hue="blue",
13
- neutral_hue="slate",
14
  )
15
 
 
16
  def main(
17
- n_clusters:int=2,
18
- n_samples:int=500,
19
- n_features:int=2,
20
- n_centers:int=4,
21
- cluster_std:int=1,
22
- ):
23
  # Generating the sample data from make_blobs
24
  # This particular setting has one distinct cluster and 3 clusters placed close
25
  # together.
@@ -31,7 +30,7 @@ def main(
31
  center_box=(-10.0, 10.0),
32
  shuffle=True,
33
  random_state=1,
34
- )
35
  # For reproducibility
36
  n_clusters = int(n_clusters)
37
  fig1, ax1 = plt.subplots()
@@ -127,9 +126,11 @@ def main(
127
  ax2.set_title("The visualization of the clustered data.")
128
  ax2.set_xlabel("Feature space for the 1st feature")
129
  ax2.set_ylabel("Feature space for the 2nd feature")
 
130
  return fig1, fig2
131
 
132
- title = '''# Selecting the number of clusters with silhouette analysis on KMeans clustering πŸ“Š'''
 
133
  description = """
134
  This app demonstrates a silhouette analysis for KMeans clustering on sample data.
135
 
@@ -139,21 +140,35 @@ description = """
139
  with gr.Blocks(theme=theme) as demo:
140
  gr.Markdown(title)
141
  gr.Markdown(description)
142
- gr.Markdown('''### Dataset Generation Parameters''')
143
  with gr.Row():
144
  with gr.Column():
145
- n_samples = gr.inputs.Slider(minimum=100, maximum=1000, default=500, step=50, label="n_samples")
146
- n_features = gr.inputs.Slider(minimum=2, maximum=5, default=2, step=1, label="n_features")
147
- n_clusters = gr.inputs.Slider(minimum=2, maximum=6, default=2, step=1, label="n_clusters")
148
- n_centers = gr.inputs.Slider(minimum=2, maximum=5, default=4, step=1, label="n_centers")
149
- cluster_std = gr.inputs.Slider(minimum=0.0, maximum=1.0, default=1, step=0.1, label="cluster_std")
150
- run_button = gr.Button('Analyse Silhouette')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  with gr.Row():
152
  plot_silhouette = gr.Plot()
153
  plot_clusters = gr.Plot()
154
  outputs = [plot_silhouette, plot_clusters]
155
- inputs = [n_clusters, n_samples,n_features,n_centers,cluster_std]
156
  run_button.click(fn=main, inputs=inputs, outputs=outputs)
157
 
158
-
159
- demo.launch()
 
8
  import numpy as np
9
 
10
  theme = gr.themes.Monochrome(
11
+ primary_hue="indigo", secondary_hue="blue", neutral_hue="slate",
 
 
12
  )
13
 
14
+
15
  def main(
16
+ n_clusters: int = 2,
17
+ n_samples: int = 500,
18
+ n_features: int = 2,
19
+ n_centers: int = 4,
20
+ cluster_std: int = 1,
21
+ ):
22
  # Generating the sample data from make_blobs
23
  # This particular setting has one distinct cluster and 3 clusters placed close
24
  # together.
 
30
  center_box=(-10.0, 10.0),
31
  shuffle=True,
32
  random_state=1,
33
+ )
34
  # For reproducibility
35
  n_clusters = int(n_clusters)
36
  fig1, ax1 = plt.subplots()
 
126
  ax2.set_title("The visualization of the clustered data.")
127
  ax2.set_xlabel("Feature space for the 1st feature")
128
  ax2.set_ylabel("Feature space for the 2nd feature")
129
+
130
  return fig1, fig2
131
 
132
+
133
+ title = """# Selecting the number of clusters with silhouette analysis on KMeans clustering πŸ“Š"""
134
  description = """
135
  This app demonstrates a silhouette analysis for KMeans clustering on sample data.
136
 
 
140
  with gr.Blocks(theme=theme) as demo:
141
  gr.Markdown(title)
142
  gr.Markdown(description)
143
+ gr.Markdown("""### Dataset Generation Parameters""")
144
  with gr.Row():
145
  with gr.Column():
146
+ n_samples = gr.inputs.Slider(
147
+ minimum=100,
148
+ maximum=1000,
149
+ default=500,
150
+ step=50,
151
+ label="Number of Samples",
152
+ )
153
+ n_features = gr.inputs.Slider(
154
+ minimum=2, maximum=5, default=2, step=1, label="Number of Features"
155
+ )
156
+ n_centers = gr.inputs.Slider(
157
+ minimum=2, maximum=5, default=4, step=1, label="Number of Centers"
158
+ )
159
+ cluster_std = gr.inputs.Slider(
160
+ minimum=0.0, maximum=1.0, default=1, step=0.1, label="Cluster deviation"
161
+ )
162
+ n_clusters = gr.inputs.Slider(
163
+ minimum=2, maximum=6, default=2, step=1, label="Number of Clusters"
164
+ )
165
+ run_button = gr.Button("Analyse Silhouette")
166
  with gr.Row():
167
  plot_silhouette = gr.Plot()
168
  plot_clusters = gr.Plot()
169
  outputs = [plot_silhouette, plot_clusters]
170
+ inputs = [n_clusters, n_samples, n_features, n_centers, cluster_std]
171
  run_button.click(fn=main, inputs=inputs, outputs=outputs)
172
 
173
+
174
+ demo.launch()