1919# SPDX-License-Identifier: MIT
2020
2121
22- #%%
22+ # %%
2323# Load the Iris dataset
2424# ---------------------
2525#
3434
3535# Load the Iris dataset
3636iris = datasets .load_iris ()
37- X = iris [' data' ]
37+ X = iris [" data" ]
3838y = iris .target
3939
4040
41- #%%
41+ # %%
4242# Visualize the Iris dataset
4343# --------------------------
4444#
5252pca = PCA (n_components = 3 )
5353iris_reduced = pca .fit_transform (X )
5454fig = plt .figure (figsize = (8 , 6 ))
55- ax = fig .add_subplot (111 , projection = '3d' , elev = 48 , azim = 134 )
55+ ax = fig .add_subplot (111 , projection = "3d" , elev = 48 , azim = 134 )
5656ax .scatter (
5757 iris_reduced [:, 0 ],
5858 iris_reduced [:, 1 ],
5959 iris_reduced [:, 2 ],
6060 c = y ,
61- cmap = ' Dark2' ,
61+ cmap = " Dark2" ,
6262 s = 40 ,
6363)
6464# Set plot labels
7474
7575plt .show ()
7676
77- #%%
77+ # %%
7878# Compute Clustering with Radius Clustering
7979# -----------------------------------------
8080#
8787rad .fit (X )
8888t_rad = time .time () - t0
8989
90- #%%
90+ # %%
9191# Compute KMeans Clustering for Comparison
9292# ----------------------------------------
9393#
100100k_means .fit (X )
101101t_kmeans = time .time () - t0
102102
103- #%% Establishing parity between clusters
103+ # %% Establishing parity between clusters
104104# --------------------------------------
105105#
106106# We want to have the same color for the same cluster in both plots.
107107# We can achieve this by matching the cluster labels of the Radius clustering and the KMeans clustering.
108108# First we define a function to retrieve the cluster centers from the Radius clustering and KMeans clustering and
109109# match them pairwise.
110110
111+
111112def get_order_labels (kmeans , rad , data ):
112113 centers1_cpy = kmeans .cluster_centers_ .copy ()
113114 centers2_cpy = data [rad .centers_ ].copy ()
@@ -133,6 +134,7 @@ def get_order_labels(kmeans, rad, data):
133134 order .append (int (match_label [0 ]))
134135 return order
135136
137+
136138from sklearn .metrics .pairwise import pairwise_distances_argmin
137139
138140rad_centers_index = np .array (rad .centers_ )
@@ -146,7 +148,7 @@ def get_order_labels(kmeans, rad, data):
146148kmeans_labels = pairwise_distances_argmin (X , kmeans_centers )
147149rad_labels = pairwise_distances_argmin (X , rad_centers_coordinates )
148150
149- #%%
151+ # %%
150152# Plotting the results and the difference
151153# ---------------------------------------
152154
@@ -162,7 +164,7 @@ def get_order_labels(kmeans, rad, data):
162164 iris_reduced [:, 1 ],
163165 iris_reduced [:, 2 ],
164166 c = kmeans_labels ,
165- cmap = ' Dark2' ,
167+ cmap = " Dark2" ,
166168 s = 40 ,
167169)
168170# adapting center coordinates to the 3D plot
@@ -214,7 +216,9 @@ def get_order_labels(kmeans, rad, data):
214216 different += (kmeans_labels == k ) != (rad_labels == k )
215217
216218identical = np .logical_not (different )
217- ax .scatter (iris_reduced [identical , 0 ], iris_reduced [identical , 1 ], color = "#bbbbbb" , marker = "." )
219+ ax .scatter (
220+ iris_reduced [identical , 0 ], iris_reduced [identical , 1 ], color = "#bbbbbb" , marker = "."
221+ )
218222ax .scatter (iris_reduced [different , 0 ], iris_reduced [different , 1 ], color = "m" )
219223ax .set_title ("Difference" )
220224ax .set_xticks (())
@@ -223,7 +227,7 @@ def get_order_labels(kmeans, rad, data):
223227
224228plt .show ()
225229
226- #%%
230+ # %%
227231# Another difference plot
228232# -----------------------
229233#
@@ -250,7 +254,7 @@ def get_order_labels(kmeans, rad, data):
250254k_means .fit (X )
251255t_kmeans = time .time () - t0
252256
253- #%%
257+ # %%
254258# Reapllying the same process as before
255259# --------------------------------------
256260
@@ -265,7 +269,7 @@ def get_order_labels(kmeans, rad, data):
265269kmeans_labels = pairwise_distances_argmin (X , kmeans_centers )
266270rad_labels = pairwise_distances_argmin (X , rad_centers_coordinates )
267271
268- #%%
272+ # %%
269273# Plotting the results and the difference
270274# ---------------------------------------
271275
@@ -281,7 +285,7 @@ def get_order_labels(kmeans, rad, data):
281285 wine_reduced [:, 1 ],
282286 wine_reduced [:, 2 ],
283287 c = kmeans_labels ,
284- cmap = ' Dark2' ,
288+ cmap = " Dark2" ,
285289 s = 40 ,
286290)
287291# adapting center coordinates to the 3D plot
@@ -298,7 +302,9 @@ def get_order_labels(kmeans, rad, data):
298302ax .set_yticks (())
299303ax .set_zticks (())
300304
301- ax .text3D (60.0 , 80.0 , 0.0 , "train time: %.2fs\n inertia: %f" % (t_kmeans , k_means .inertia_ ))
305+ ax .text3D (
306+ 60.0 , 80.0 , 0.0 , "train time: %.2fs\n inertia: %f" % (t_kmeans , k_means .inertia_ )
307+ )
302308
303309# MDS
304310ax = fig .add_subplot (1 , 3 , 2 , projection = "3d" , elev = 48 , azim = 134 , roll = 0 )
@@ -333,7 +339,9 @@ def get_order_labels(kmeans, rad, data):
333339 different += (kmeans_labels == k ) != (rad_labels == k )
334340
335341identical = np .logical_not (different )
336- ax .scatter (wine_reduced [identical , 0 ], wine_reduced [identical , 1 ], color = "#bbbbbb" , marker = "." )
342+ ax .scatter (
343+ wine_reduced [identical , 0 ], wine_reduced [identical , 1 ], color = "#bbbbbb" , marker = "."
344+ )
337345ax .scatter (wine_reduced [different , 0 ], wine_reduced [different , 1 ], color = "m" )
338346ax .set_title ("Difference" )
339347ax .set_xticks (())
@@ -342,7 +350,7 @@ def get_order_labels(kmeans, rad, data):
342350
343351plt .show ()
344352
345- #%%
353+ # %%
346354# Conclusion
347355# ----------
348356#
0 commit comments