Skip to content

Commit bc8d54b

Browse files
Re-applied Spectral clustering on time series data using Aeon
1 parent 50ca0e8 commit bc8d54b

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

Diff for: examples/clustering/sklearn_clustering_with_aeon_distances.ipynb

+9-12
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
{
2626
"cell_type": "code",
27-
"execution_count": 106,
27+
"execution_count": null,
2828
"metadata": {},
2929
"outputs": [
3030
{
@@ -59,7 +59,7 @@
5959
},
6060
{
6161
"cell_type": "code",
62-
"execution_count": 107,
62+
"execution_count": null,
6363
"metadata": {},
6464
"outputs": [
6565
{
@@ -267,7 +267,7 @@
267267
},
268268
{
269269
"cell_type": "code",
270-
"execution_count": 138,
270+
"execution_count": null,
271271
"metadata": {},
272272
"outputs": [
273273
{
@@ -284,18 +284,15 @@
284284
"source": [
285285
"import matplotlib.pyplot as plt\n",
286286
"import numpy as np\n",
287-
"from sklearn.cluster import SpectralClustering\n",
288-
"from sklearn.metrics import pairwise_distances\n",
289-
"\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",
287+
"X, _ = load_basic_motions(split=\"train\", return_X_y=True)\n",
288+
"flat_X = np.array([flatten(ts) for ts in X])\n",
289+
"distance_matrix = pairwise_distances(flat_X, metric=\"euclidean\")\n",
293290
"inverse_distance_matrix = 1 - (distance_matrix / distance_matrix.max())\n",
294291
"spectral = SpectralClustering(n_clusters=2, affinity=\"precomputed\", random_state=42)\n",
295-
"spectral_labels = spectral.fit_predict(inverse_distance_matrix)\n",
292+
"labels = spectral.fit_predict(inverse_distance_matrix)\n",
296293
"plt.figure(figsize=(10, 6))\n",
297-
"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",
294+
"for label in np.unique(labels):\n",
295+
" plt.plot(flat_X[labels == label].T, alpha=0.5, label=f\"Cluster {label}\")\n",
299296
"plt.title(\"Spectral Clustering with Normalized Similarity Matrix\")\n",
300297
"plt.legend()\n",
301298
"plt.show()"

0 commit comments

Comments
 (0)