Skip to content

Commit

Permalink
Automatic pre-commit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmanDeveloperz authored and github-actions[bot] committed Mar 9, 2025
1 parent 50ca0e8 commit 91562a7
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions examples/clustering/sklearn_clustering_with_aeon_distances.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@
"from sklearn.cluster import AgglomerativeClustering\n",
"\n",
"# Perform Agglomerative Clustering\n",
"agg_clustering = AgglomerativeClustering(n_clusters=2, metric=\"precomputed\", linkage=\"average\")\n",
"agg_clustering = AgglomerativeClustering(\n",
" n_clusters=2, metric=\"precomputed\", linkage=\"average\"\n",
")\n",
"labels = agg_clustering.fit_predict(distance_matrix)\n",
"\n",
"# Visualize the clustering results\n",
Expand Down Expand Up @@ -181,7 +183,7 @@
"plt.figure(figsize=(10, 6))\n",
"for label in np.unique(dbscan_labels):\n",
" cluster_data = X[np.where(dbscan_labels == label)] # Fix indexing\n",
" \n",
"\n",
" if label == -1:\n",
" plt.plot(cluster_data.mean(axis=0), label=\"Noise\", linestyle=\"--\", linewidth=2)\n",
" else:\n",
Expand All @@ -192,7 +194,7 @@
"plt.ylabel(\"Mean Value\")\n",
"plt.legend(loc=\"upper right\", fontsize=\"small\")\n",
"plt.grid(True)\n",
"plt.show()\n"
"plt.show()"
]
},
{
Expand Down Expand Up @@ -239,22 +241,27 @@
"\n",
" # Ensure correct shape for plotting\n",
" cluster_data = np.squeeze(cluster_data)\n",
" if cluster_data.ndim == 1: \n",
" if cluster_data.ndim == 1:\n",
" cluster_data = cluster_data[:, np.newaxis] # Convert to 2D if needed\n",
"\n",
" # Compute mean representation of each cluster\n",
" cluster_mean = cluster_data.mean(axis=0) \n",
" cluster_mean = cluster_data.mean(axis=0)\n",
"\n",
" # Plot noise separately\n",
" if label == -1:\n",
" plt.plot(cluster_mean, linestyle=\"--\", color=\"gray\", alpha=0.5, label=\"Noise\")\n",
" else:\n",
" plt.plot(cluster_mean, color=colors(label % colors.N), alpha=0.7, label=f\"Cluster {label}\")\n",
" plt.plot(\n",
" cluster_mean,\n",
" color=colors(label % colors.N),\n",
" alpha=0.7,\n",
" label=f\"Cluster {label}\",\n",
" )\n",
"\n",
"plt.title(\"OPTICS Clustering with DTW Distance\")\n",
"plt.legend()\n",
"plt.grid(True, linestyle=\"--\", alpha=0.5) # Light grid for better readability\n",
"plt.show()\n"
"plt.show()"
]
},
{
Expand Down Expand Up @@ -287,15 +294,24 @@
"from sklearn.cluster import SpectralClustering\n",
"from sklearn.metrics import pairwise_distances\n",
"\n",
"X = np.vstack((np.random.normal(loc=[2, 2], scale=0.5, size=(50, 2)), \n",
" np.random.normal(loc=[5, 5], scale=0.5, size=(50, 2))))\n",
"distance_matrix = pairwise_distances(X, metric='euclidean')\n",
"X = np.vstack(\n",
" (\n",
" np.random.normal(loc=[2, 2], scale=0.5, size=(50, 2)),\n",
" np.random.normal(loc=[5, 5], scale=0.5, size=(50, 2)),\n",
" )\n",
")\n",
"distance_matrix = pairwise_distances(X, metric=\"euclidean\")\n",
"inverse_distance_matrix = 1 - (distance_matrix / distance_matrix.max())\n",
"spectral = SpectralClustering(n_clusters=2, affinity=\"precomputed\", random_state=42)\n",
"spectral_labels = spectral.fit_predict(inverse_distance_matrix)\n",
"plt.figure(figsize=(10, 6))\n",
"for label in np.unique(spectral_labels):\n",
" plt.scatter(X[spectral_labels == label, 0], X[spectral_labels == label, 1], label=f\"Cluster {label}\", alpha=0.7)\n",
" plt.scatter(\n",
" X[spectral_labels == label, 0],\n",
" X[spectral_labels == label, 1],\n",
" label=f\"Cluster {label}\",\n",
" alpha=0.7,\n",
" )\n",
"plt.title(\"Spectral Clustering with Normalized Similarity Matrix\")\n",
"plt.legend()\n",
"plt.show()"
Expand Down

0 comments on commit 91562a7

Please sign in to comment.