|
24 | 24 | },
|
25 | 25 | {
|
26 | 26 | "cell_type": "code",
|
27 |
| - "execution_count": 106, |
| 27 | + "execution_count": null, |
28 | 28 | "metadata": {},
|
29 | 29 | "outputs": [
|
30 | 30 | {
|
|
59 | 59 | },
|
60 | 60 | {
|
61 | 61 | "cell_type": "code",
|
62 |
| - "execution_count": 107, |
| 62 | + "execution_count": null, |
63 | 63 | "metadata": {},
|
64 | 64 | "outputs": [
|
65 | 65 | {
|
|
267 | 267 | },
|
268 | 268 | {
|
269 | 269 | "cell_type": "code",
|
270 |
| - "execution_count": 138, |
| 270 | + "execution_count": null, |
271 | 271 | "metadata": {},
|
272 | 272 | "outputs": [
|
273 | 273 | {
|
|
284 | 284 | "source": [
|
285 | 285 | "import matplotlib.pyplot as plt\n",
|
286 | 286 | "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", |
293 | 290 | "inverse_distance_matrix = 1 - (distance_matrix / distance_matrix.max())\n",
|
294 | 291 | "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", |
296 | 293 | "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", |
299 | 296 | "plt.title(\"Spectral Clustering with Normalized Similarity Matrix\")\n",
|
300 | 297 | "plt.legend()\n",
|
301 | 298 | "plt.show()"
|
|
0 commit comments