style: amélioration de la mise en forme et des espaces dans le code

This commit is contained in:
2025-12-02 16:51:19 +01:00
parent 33930ab89c
commit 82fb7e53de

View File

@@ -14,7 +14,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": null,
"id": "c1b23a23",
"metadata": {},
"outputs": [],
@@ -24,6 +24,7 @@
" D = np.diag(np.sum(W, axis=1))\n",
" return D - W\n",
"\n",
"\n",
"def compute_symmetric_laplacian(W: np.ndarray) -> np.ndarray:\n",
" \"\"\"Compute the symmetric normalized graph Laplacian.\"\"\"\n",
" D_inv_sqrt = np.diag(1.0 / np.sqrt(np.sum(W, axis=1)))\n",
@@ -35,24 +36,28 @@
" D_inv = np.diag(1.0 / np.sum(W, axis=1))\n",
" return np.eye(W.shape[0]) - D_inv @ W\n",
"\n",
"\n",
"def sort_eigenvalues(M: np.ndarray, k: int):\n",
" \"\"\"Sort eigenvalues and eigenvectors in ascending order.\"\"\"\n",
" eigenvalues, eigenvectors = np.linalg.eigh(M)\n",
" idx_L = np.argsort(eigenvalues)[:k]\n",
" return eigenvalues[idx_L], eigenvectors[:, idx_L]\n",
"\n",
"\n",
"def compute_W_matrix(sigma: int, X: np.ndarray) -> np.ndarray:\n",
" \"\"\"Fill the similarity matrix W.\"\"\"\n",
" W = np.zeros((len(X), len(X)))\n",
" for i in range(len(X)):\n",
" for j in range(len(X)):\n",
" W[i, j] = 0 if i == j else np.exp(-(np.abs(X[i] - X[j]) ** 2) / (2 * sigma**2))\n",
" return W"
" W[i, j] = (\n",
" 0 if i == j else np.exp(-(np.abs(X[i] - X[j]) ** 2) / (2 * sigma**2))\n",
" )\n",
" return W\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": null,
"id": "9e7e4ffa",
"metadata": {},
"outputs": [],
@@ -67,6 +72,7 @@
"\n",
" return np.concatenate([norm_1, norm_2, norm_3, norm_4])\n",
"\n",
"\n",
"def plot_eigenvalues(sigma: int, n: int, k: int):\n",
" \"\"\"Plot the eigenvalues of the Laplacian for different sigma values.\"\"\"\n",
" print(f\"Plotting the eigenvalues and the distribution for Sigma: {sigma}\")\n",
@@ -87,12 +93,12 @@
" plt.xlabel(\"Index\")\n",
" plt.ylabel(\"Eigenvalue\")\n",
"\n",
" plt.show()"
" plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": null,
"id": "b3871450",
"metadata": {},
"outputs": [
@@ -168,13 +174,13 @@
"source": [
"k = 10\n",
"n = 200\n",
"for sigma in [0.1, 1/4, 0.5, 1]:\n",
" plot_eigenvalues(sigma, n, k)"
"for sigma in [0.1, 1 / 4, 0.5, 1]:\n",
" plot_eigenvalues(sigma, n, k)\n"
]
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": null,
"id": "0fb32d82",
"metadata": {},
"outputs": [],
@@ -190,12 +196,12 @@
"\n",
" kmeans = KMeans(n_clusters=k)\n",
"\n",
" return X, kmeans.fit_predict(U_normalized)"
" return X, kmeans.fit_predict(U_normalized)\n"
]
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": null,
"id": "57c342d0",
"metadata": {},
"outputs": [
@@ -229,11 +235,11 @@
}
],
"source": [
"X, clusters = spectral_clustering(1/4, k=4)\n",
"X, clusters = spectral_clustering(1 / 4, k=4)\n",
"\n",
"plt.scatter(X, np.zeros_like(X), c=clusters, cmap=\"magma\")\n",
"plt.title(\"Spectral Clustering Results\")\n",
"plt.xlabel(\"Data Points\")"
"plt.xlabel(\"Data Points\")\n"
]
},
{