hydraadra112 commited on
Commit
28c7cae
·
1 Parent(s): 6074a2e

Added interactive plots

Browse files
Files changed (1) hide show
  1. app.py +74 -21
app.py CHANGED
@@ -163,15 +163,44 @@ def main():
163
 
164
  st.write(f'Silhouette Score of K-Means: {silhouette_score(preprocessed_df, km_preds):2f}%')
165
 
166
- fig, ax = plt.subplots(figsize=(10,7))
167
- ax.set_title('KMeans Algorithm')
168
- ax.scatter(km_centroids[:,0], km_centroids[:,1], s=200, c='black', alpha=0.5, label='Centroids')
169
- scatter = ax.scatter(preprocessed_df[:,0], preprocessed_df[:,1], c=km_preds, s=20)
170
- plt.scatter(input_data[0], input_data[1], s=150, c='red', marker='X', label='Input Data')
171
- plt.legend()
172
- plt.colorbar(scatter, ax=ax, label='Cluster Labels')
173
- ax.grid(True)
174
- st.pyplot(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
  url = 'https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html'
177
  st.caption('For more details of the hyperparameters, check out the the documentation of KMeans [source](%s) of the dataset.' % url)
@@ -201,12 +230,24 @@ def main():
201
  else:
202
  st.error('Cannot calculate Silhouette Score with unscaled data.')
203
 
204
- fig, ax = plt.subplots(figsize=(10,7))
205
- ax.set_title('DBSCAN Model')
206
- scatter = ax.scatter(preprocessed_df[:, 0], preprocessed_df[:, 1], s=20, c=dbs_labels, cmap='viridis')
207
- ax.grid(True)
208
- plt.colorbar(scatter, ax=ax, label='Cluster Labels')
209
- st.pyplot(fig)
 
 
 
 
 
 
 
 
 
 
 
 
210
 
211
  url = 'https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html'
212
  st.caption('For more details of the hyperparameters, check out the the documentation of DBSCAN [source](%s) of the dataset.' % url)
@@ -244,13 +285,25 @@ def main():
244
  st.write(f'Silhouette Score of Spectral Clustering: {silhouette_score(preprocessed_df, km_preds):2f}%')
245
  else:
246
  st.error('Cannot calculate Silhouette Score with unscaled data.')
 
 
247
 
248
- fig, ax = plt.subplots(figsize=(10,7))
249
- scatter = ax.scatter(preprocessed_df[:, 0], preprocessed_df[:, 1], c=sc_labels, s=20)
250
- ax.set_title('Spectral Clustering')
251
- plt.colorbar(scatter, ax=ax, label='Cluster Labels')
252
- plt.grid(True)
253
- st.pyplot(fig)
 
 
 
 
 
 
 
 
 
 
254
 
255
  url = 'https://scikit-learn.org/stable/modules/generated/sklearn.cluster.SpectralClustering.html'
256
  st.caption('For more details of the hyperparameters, check out the the documentation of Spectral Clustering [source](%s) of the dataset.' % url)
 
163
 
164
  st.write(f'Silhouette Score of K-Means: {silhouette_score(preprocessed_df, km_preds):2f}%')
165
 
166
+ tab1, tab2 = st.tabs(['Matplotlib Plot', 'Interactive Plot'])
167
+
168
+ with tab1:
169
+ fig, ax = plt.subplots(figsize=(10,7))
170
+ ax.set_title('KMeans Algorithm')
171
+ ax.scatter(km_centroids[:,0], km_centroids[:,1], s=200, c='black', alpha=0.5, label='Centroids')
172
+ scatter = ax.scatter(preprocessed_df[:,0], preprocessed_df[:,1], c=km_preds, s=20)
173
+ plt.scatter(input_data[0], input_data[1], s=150, c='red', marker='X', label='Input Data')
174
+ plt.legend()
175
+ plt.colorbar(scatter, ax=ax, label='Cluster Labels')
176
+ ax.grid(True)
177
+ st.pyplot(fig)
178
+
179
+ with tab2:
180
+ data_df = pd.DataFrame({
181
+ 'x': preprocessed_df[:, 0],
182
+ 'y': preprocessed_df[:, 1],
183
+ 'Type': ['Data Point'] * len(preprocessed_df),
184
+ 'Cluster': km_preds
185
+ })
186
+
187
+ centroids_df = pd.DataFrame({
188
+ 'x': km_centroids[:, 0],
189
+ 'y': km_centroids[:, 1],
190
+ 'Type': ['Centroid'] * len(km_centroids),
191
+ 'Cluster': ['Centroid'] * len(km_centroids)
192
+ })
193
+
194
+ input_df = pd.DataFrame({
195
+ 'x': [input_data[0]],
196
+ 'y': [input_data[1]],
197
+ 'Type': ['Input Data'],
198
+ 'Cluster': ['Input Data']
199
+ })
200
+
201
+ plot_df = pd.concat([data_df, centroids_df, input_df])
202
+
203
+ st.scatter_chart(plot_df, x='x', y='y', color='Cluster', size='Type')
204
 
205
  url = 'https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html'
206
  st.caption('For more details of the hyperparameters, check out the the documentation of KMeans [source](%s) of the dataset.' % url)
 
230
  else:
231
  st.error('Cannot calculate Silhouette Score with unscaled data.')
232
 
233
+ tab1, tab2 = st.tabs(['Matplotlib Plot', 'Interactive Plot'])
234
+
235
+ with tab1:
236
+ fig, ax = plt.subplots(figsize=(10,7))
237
+ ax.set_title('DBSCAN Model')
238
+ scatter = ax.scatter(preprocessed_df[:, 0], preprocessed_df[:, 1], s=20, c=dbs_labels, cmap='viridis')
239
+ ax.grid(True)
240
+ plt.colorbar(scatter, ax=ax, label='Cluster Labels')
241
+ st.pyplot(fig)
242
+
243
+ with tab2:
244
+ scatter_df = pd.DataFrame({
245
+ 'x': preprocessed_df[:, 0],
246
+ 'y': preprocessed_df[:, 1],
247
+ 'Cluster': dbs_labels
248
+ })
249
+
250
+ st.scatter_chart(scatter_df, x='x', y='y', color='Cluster')
251
 
252
  url = 'https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html'
253
  st.caption('For more details of the hyperparameters, check out the the documentation of DBSCAN [source](%s) of the dataset.' % url)
 
285
  st.write(f'Silhouette Score of Spectral Clustering: {silhouette_score(preprocessed_df, km_preds):2f}%')
286
  else:
287
  st.error('Cannot calculate Silhouette Score with unscaled data.')
288
+
289
+ tab1, tab2 = st.tabs(['Matplotlib Plot', 'Interactive Plot'])
290
 
291
+ with tab1:
292
+ fig, ax = plt.subplots(figsize=(10,7))
293
+ scatter = ax.scatter(preprocessed_df[:, 0], preprocessed_df[:, 1], c=sc_labels, s=20)
294
+ ax.set_title('Spectral Clustering')
295
+ plt.colorbar(scatter, ax=ax, label='Cluster Labels')
296
+ plt.grid(True)
297
+ st.pyplot(fig)
298
+
299
+ with tab2:
300
+ scatter_df = pd.DataFrame({
301
+ 'x': preprocessed_df[:, 0],
302
+ 'y': preprocessed_df[:, 1],
303
+ 'Cluster': sc_labels
304
+ })
305
+
306
+ st.scatter_chart(scatter_df, x='x', y='y', color='Cluster')
307
 
308
  url = 'https://scikit-learn.org/stable/modules/generated/sklearn.cluster.SpectralClustering.html'
309
  st.caption('For more details of the hyperparameters, check out the the documentation of Spectral Clustering [source](%s) of the dataset.' % url)