|
119 | 119 | "from sklearn.cluster import AgglomerativeClustering\n",
|
120 | 120 | "\n",
|
121 | 121 | "# Perform Agglomerative Clustering\n",
|
122 |
| - "agg_clustering = AgglomerativeClustering(n_clusters=2, metric=\"precomputed\", linkage=\"average\")\n", |
| 122 | + "agg_clustering = AgglomerativeClustering(\n", |
| 123 | + " n_clusters=2, metric=\"precomputed\", linkage=\"average\"\n", |
| 124 | + ")\n", |
123 | 125 | "labels = agg_clustering.fit_predict(distance_matrix)\n",
|
124 | 126 | "\n",
|
125 | 127 | "# Visualize the clustering results\n",
|
|
181 | 183 | "plt.figure(figsize=(10, 6))\n",
|
182 | 184 | "for label in np.unique(dbscan_labels):\n",
|
183 | 185 | " cluster_data = X[np.where(dbscan_labels == label)] # Fix indexing\n",
|
184 |
| - " \n", |
| 186 | + "\n", |
185 | 187 | " if label == -1:\n",
|
186 | 188 | " plt.plot(cluster_data.mean(axis=0), label=\"Noise\", linestyle=\"--\", linewidth=2)\n",
|
187 | 189 | " else:\n",
|
|
192 | 194 | "plt.ylabel(\"Mean Value\")\n",
|
193 | 195 | "plt.legend(loc=\"upper right\", fontsize=\"small\")\n",
|
194 | 196 | "plt.grid(True)\n",
|
195 |
| - "plt.show()\n" |
| 197 | + "plt.show()" |
196 | 198 | ]
|
197 | 199 | },
|
198 | 200 | {
|
|
239 | 241 | "\n",
|
240 | 242 | " # Ensure correct shape for plotting\n",
|
241 | 243 | " cluster_data = np.squeeze(cluster_data)\n",
|
242 |
| - " if cluster_data.ndim == 1: \n", |
| 244 | + " if cluster_data.ndim == 1:\n", |
243 | 245 | " cluster_data = cluster_data[:, np.newaxis] # Convert to 2D if needed\n",
|
244 | 246 | "\n",
|
245 | 247 | " # Compute mean representation of each cluster\n",
|
246 |
| - " cluster_mean = cluster_data.mean(axis=0) \n", |
| 248 | + " cluster_mean = cluster_data.mean(axis=0)\n", |
247 | 249 | "\n",
|
248 | 250 | " # Plot noise separately\n",
|
249 | 251 | " if label == -1:\n",
|
250 | 252 | " plt.plot(cluster_mean, linestyle=\"--\", color=\"gray\", alpha=0.5, label=\"Noise\")\n",
|
251 | 253 | " else:\n",
|
252 |
| - " plt.plot(cluster_mean, color=colors(label % colors.N), alpha=0.7, label=f\"Cluster {label}\")\n", |
| 254 | + " plt.plot(\n", |
| 255 | + " cluster_mean,\n", |
| 256 | + " color=colors(label % colors.N),\n", |
| 257 | + " alpha=0.7,\n", |
| 258 | + " label=f\"Cluster {label}\",\n", |
| 259 | + " )\n", |
253 | 260 | "\n",
|
254 | 261 | "plt.title(\"OPTICS Clustering with DTW Distance\")\n",
|
255 | 262 | "plt.legend()\n",
|
256 | 263 | "plt.grid(True, linestyle=\"--\", alpha=0.5) # Light grid for better readability\n",
|
257 |
| - "plt.show()\n" |
| 264 | + "plt.show()" |
258 | 265 | ]
|
259 | 266 | },
|
260 | 267 | {
|
|
287 | 294 | "from sklearn.cluster import SpectralClustering\n",
|
288 | 295 | "from sklearn.metrics import pairwise_distances\n",
|
289 | 296 | "\n",
|
290 |
| - "X = np.vstack((np.random.normal(loc=[2, 2], scale=0.5, size=(50, 2)), \n", |
291 |
| - " np.random.normal(loc=[5, 5], scale=0.5, size=(50, 2))))\n", |
292 |
| - "distance_matrix = pairwise_distances(X, metric='euclidean')\n", |
| 297 | + "X = np.vstack(\n", |
| 298 | + " (\n", |
| 299 | + " np.random.normal(loc=[2, 2], scale=0.5, size=(50, 2)),\n", |
| 300 | + " np.random.normal(loc=[5, 5], scale=0.5, size=(50, 2)),\n", |
| 301 | + " )\n", |
| 302 | + ")\n", |
| 303 | + "distance_matrix = pairwise_distances(X, metric=\"euclidean\")\n", |
293 | 304 | "inverse_distance_matrix = 1 - (distance_matrix / distance_matrix.max())\n",
|
294 | 305 | "spectral = SpectralClustering(n_clusters=2, affinity=\"precomputed\", random_state=42)\n",
|
295 | 306 | "spectral_labels = spectral.fit_predict(inverse_distance_matrix)\n",
|
296 | 307 | "plt.figure(figsize=(10, 6))\n",
|
297 | 308 | "for label in np.unique(spectral_labels):\n",
|
298 |
| - " plt.scatter(X[spectral_labels == label, 0], X[spectral_labels == label, 1], label=f\"Cluster {label}\", alpha=0.7)\n", |
| 309 | + " plt.scatter(\n", |
| 310 | + " X[spectral_labels == label, 0],\n", |
| 311 | + " X[spectral_labels == label, 1],\n", |
| 312 | + " label=f\"Cluster {label}\",\n", |
| 313 | + " alpha=0.7,\n", |
| 314 | + " )\n", |
299 | 315 | "plt.title(\"Spectral Clustering with Normalized Similarity Matrix\")\n",
|
300 | 316 | "plt.legend()\n",
|
301 | 317 | "plt.show()"
|
|
0 commit comments