Refactor code for improved readability and consistency across notebooks

- Standardized spacing around operators and function arguments in TP7_Kmeans.ipynb and neural_network.ipynb.
- Enhanced the formatting of model building and training code in neural_network.ipynb for better clarity.
- Updated the pyproject.toml to remove a specific TensorFlow version and added linting configuration for Ruff.
- Improved comments and organization in the code to facilitate easier understanding and maintenance.
This commit is contained in:
2025-07-01 20:46:08 +02:00
parent e273cf90f7
commit f94ff07cab
34 changed files with 5713 additions and 5047 deletions

View File

@@ -28,8 +28,9 @@
"k = np.arange(1, 12 + 1)\n",
"m = np.power(2, k)\n",
"\n",
"\n",
"def f(x):\n",
"\treturn 1 / np.sqrt(x)"
" return 1 / np.sqrt(x)"
]
},
{
@@ -39,19 +40,23 @@
"outputs": [],
"source": [
"a, b = 1, 2\n",
"\n",
"\n",
"def compute_I(f, a, b, m):\n",
" h_list = (b - a) / m\n",
" I = []\n",
" errors = []\n",
" sol_exact = quad(f, a, b)[0]\n",
" \n",
"\n",
" for h in h_list:\n",
" t = np.arange(a, b, h)\n",
" y = np.array([3/4 * h * f(t[i] + h/3) + h/4 * f(t[i] + h) for i in range(len(t))])\n",
" y = np.array(\n",
" [3 / 4 * h * f(t[i] + h / 3) + h / 4 * f(t[i] + h) for i in range(len(t))]\n",
" )\n",
" I_approx = np.sum(y)\n",
" I.append(I_approx)\n",
" errors.append(np.abs(I_approx - sol_exact))\n",
" \n",
"\n",
" return I, h_list, errors"
]
},
@@ -84,12 +89,12 @@
"print(f\"I1 = {I1}\")\n",
"\n",
"plt.figure(figsize=(10, 5))\n",
"plt.plot(np.log(h_list), np.log(errors1), 'o-', label='Erreur numérique')\n",
"plt.plot(np.log(h_list), 2*np.log(h_list), '--', label='Ordre 2')\n",
"plt.plot(np.log(h_list), 4*np.log(h_list), '--', label='Ordre 4')\n",
"plt.xlabel('log(h)')\n",
"plt.ylabel('log(Erreur)')\n",
"plt.title('Convergence de la méthode d\\'intégration')\n",
"plt.plot(np.log(h_list), np.log(errors1), \"o-\", label=\"Erreur numérique\")\n",
"plt.plot(np.log(h_list), 2 * np.log(h_list), \"--\", label=\"Ordre 2\")\n",
"plt.plot(np.log(h_list), 4 * np.log(h_list), \"--\", label=\"Ordre 4\")\n",
"plt.xlabel(\"log(h)\")\n",
"plt.ylabel(\"log(Erreur)\")\n",
"plt.title(\"Convergence de la méthode d'intégration\")\n",
"plt.legend()\n",
"plt.grid(True)\n",
"plt.show()"
@@ -116,12 +121,12 @@
"I2, h_list, errors2 = compute_I(f, a, b, m)\n",
"\n",
"plt.figure(figsize=(10, 5))\n",
"plt.plot(np.log(h_list), np.log(errors2), label='Approximation de l\\'intégrale')\n",
"plt.plot(np.log(h_list), np.log(h_list), '--', label='h')\n",
"plt.plot(np.log(h_list), 2*np.log(h_list), '--', label='h^2')\n",
"plt.xlabel('h')\n",
"plt.ylabel('Approximation de l\\'intégrale')\n",
"plt.title('Approximation de l\\'intégrale par la méthode de Simpson')\n",
"plt.plot(np.log(h_list), np.log(errors2), label=\"Approximation de l'intégrale\")\n",
"plt.plot(np.log(h_list), np.log(h_list), \"--\", label=\"h\")\n",
"plt.plot(np.log(h_list), 2 * np.log(h_list), \"--\", label=\"h^2\")\n",
"plt.xlabel(\"h\")\n",
"plt.ylabel(\"Approximation de l'intégrale\")\n",
"plt.title(\"Approximation de l'intégrale par la méthode de Simpson\")\n",
"plt.legend()\n",
"plt.show()"
]
@@ -146,19 +151,19 @@
"metadata": {},
"outputs": [],
"source": [
"def RKI(f, y0, vt, tol = 1e-6, itmax = 20):\n",
"\tN, T = len(vt), vt[-1]\n",
"\tyn = np.zeros((len(y0), N))\n",
"\tyn[:, 0] = y0\n",
"\th = T / N\n",
"def RKI(f, y0, vt, tol=1e-6, itmax=20):\n",
" N, T = len(vt), vt[-1]\n",
" yn = np.zeros((len(y0), N))\n",
" yn[:, 0] = y0\n",
" h = T / N\n",
"\n",
"\tfor n in range(N-1):\n",
"\t\tp1 = f(vt[n], yn[:, n])\n",
"\t\tF1 = lambda p2: f(vt[n] + h/3, yn[:, n] + h/6 * (p1 + p2)) - p2\n",
"\t\tp2 = newton(F1, yn[:, n], fprime=None, tol=tol, maxiter=itmax)\n",
"\t\tF2 = lambda yn1: yn[:, n] + h/4 * (3 * p2 + f(vt[n+1], yn1)) - yn1\n",
"\t\tyn[:, n + 1] = newton(F2, yn[:, n], fprime=None, tol=tol, maxiter=itmax)\n",
"\treturn yn"
" for n in range(N - 1):\n",
" p1 = f(vt[n], yn[:, n])\n",
" F1 = lambda p2: f(vt[n] + h / 3, yn[:, n] + h / 6 * (p1 + p2)) - p2\n",
" p2 = newton(F1, yn[:, n], fprime=None, tol=tol, maxiter=itmax)\n",
" F2 = lambda yn1: yn[:, n] + h / 4 * (3 * p2 + f(vt[n + 1], yn1)) - yn1\n",
" yn[:, n + 1] = newton(F2, yn[:, n], fprime=None, tol=tol, maxiter=itmax)\n",
" return yn"
]
},
{
@@ -194,14 +199,18 @@
"source": [
"a, b = [0, 2]\n",
"\n",
"\n",
"def f(t, y):\n",
" return t * np.power(y, 3) - t * y\n",
" \n",
"\n",
"\n",
"y0 = [0.5]\n",
"\n",
"\n",
"def sol_exact(t):\n",
" return 1 / np.sqrt(1 + 3 * np.exp(np.power(t, 2)))\n",
"\n",
"\n",
"x_fine = np.linspace(a, b, 1000)\n",
"y_fine = sol_exact(x_fine)\n",
"\n",
@@ -212,13 +221,13 @@
"y_exact_interp = np.interp(vt, x_fine, y_fine)\n",
"\n",
"plt.figure(figsize=(10, 5))\n",
"plt.plot(x_fine, y_fine, label='Solution exacte')\n",
"plt.scatter(vt, y, label='Solution numérique', color='red')\n",
"plt.plot(x_fine, y_fine, label=\"Solution exacte\")\n",
"plt.scatter(vt, y, label=\"Solution numérique\", color=\"red\")\n",
"plt.legend()\n",
"plt.show()\n",
"\n",
"error = np.max(np.abs(y - y_exact_interp))\n",
"print(f\"Error with h={h}: {error}\")\n"
"print(f\"Error with h={h}: {error}\")"
]
},
{
@@ -246,7 +255,7 @@
],
"source": [
"k = np.arange(1, 10 + 1)\n",
"h_list = 1/np.power(2, k)\n",
"h_list = 1 / np.power(2, k)\n",
"\n",
"errors = []\n",
"for h in h_list:\n",
@@ -258,14 +267,14 @@
"log_h = np.log(h_list)\n",
"log_errors = np.log(errors)\n",
"order = np.polyfit(log_h, log_errors, 1)[0]\n",
" \n",
"\n",
"plt.figure(figsize=(10, 5))\n",
"plt.plot(log_h, log_errors, 'o-', label=f'Erreur (ordre {order:.2f})')\n",
"plt.plot(log_h, log_h, '--', label='h')\n",
"plt.plot(log_h, 2*log_h, '--', label='h^2')\n",
"plt.plot(log_h, 4*log_h, '--', label='h^4')\n",
"plt.xlabel('log(h)')\n",
"plt.ylabel('log(error)')\n",
"plt.plot(log_h, log_errors, \"o-\", label=f\"Erreur (ordre {order:.2f})\")\n",
"plt.plot(log_h, log_h, \"--\", label=\"h\")\n",
"plt.plot(log_h, 2 * log_h, \"--\", label=\"h^2\")\n",
"plt.plot(log_h, 4 * log_h, \"--\", label=\"h^4\")\n",
"plt.xlabel(\"log(h)\")\n",
"plt.ylabel(\"log(error)\")\n",
"plt.legend()\n",
"plt.grid(True)\n",
"plt.show()\n",
@@ -306,11 +315,14 @@
"source": [
"def F(t, Y):\n",
" x, y, z = Y\n",
" return np.array([\n",
" 1 + np.power(x, 2) * y - (z + 1) * x,\n",
" x * z - np.power(x, 2) * y,\n",
" - x * z + 1.45\n",
" ])\n",
" return np.array(\n",
" [\n",
" 1 + np.power(x, 2) * y - (z + 1) * x,\n",
" x * z - np.power(x, 2) * y,\n",
" -x * z + 1.45,\n",
" ]\n",
" )\n",
"\n",
"\n",
"h = 0.025\n",
"y0 = np.array([1, 1, 1])\n",
@@ -320,20 +332,20 @@
"y = RKI(F, y0, t)\n",
"fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 5))\n",
"\n",
"ax1.scatter(y[0], y[1], label='Solution numérique', color='red')\n",
"ax1.plot(sol_exact[:, 0], sol_exact[:, 1], label='Solution exacte', color='blue')\n",
"ax1.scatter(y[0], y[1], label=\"Solution numérique\", color=\"red\")\n",
"ax1.plot(sol_exact[:, 0], sol_exact[:, 1], label=\"Solution exacte\", color=\"blue\")\n",
"ax1.legend()\n",
"ax1.set_title('x vs y')\n",
"ax1.set_title(\"x vs y\")\n",
"\n",
"ax2.scatter(y[1], y[2], label='Solution numérique', color='red')\n",
"ax2.plot(sol_exact[:, 1], sol_exact[:, 2], label='Solution exacte', color='blue')\n",
"ax2.scatter(y[1], y[2], label=\"Solution numérique\", color=\"red\")\n",
"ax2.plot(sol_exact[:, 1], sol_exact[:, 2], label=\"Solution exacte\", color=\"blue\")\n",
"ax2.legend()\n",
"ax2.set_title('y vs z')\n",
"ax2.set_title(\"y vs z\")\n",
"\n",
"ax3.scatter(y[0], y[2], label='Solution numérique', color='red')\n",
"ax3.plot(sol_exact[:, 0], sol_exact[:, 2], label='Solution exacte', color='blue')\n",
"ax3.scatter(y[0], y[2], label=\"Solution numérique\", color=\"red\")\n",
"ax3.plot(sol_exact[:, 0], sol_exact[:, 2], label=\"Solution exacte\", color=\"blue\")\n",
"ax3.legend()\n",
"ax3.set_title('x vs z')\n",
"ax3.set_title(\"x vs z\")\n",
"\n",
"plt.tight_layout()\n",
"plt.show()"
@@ -357,14 +369,15 @@
],
"source": [
"def R(z):\n",
" return (1 + 3/4 * z * (1 + z/6)/(1 - z/6)) / (1 - z/4)\n",
" return (1 + 3 / 4 * z * (1 + z / 6) / (1 - z / 6)) / (1 - z / 4)\n",
"\n",
"\n",
"x = np.linspace(-15, 5, 100)\n",
"y = np.linspace(-7.5, 7.5, 100)\n",
"X, Y = np.meshgrid(x, y)\n",
"Z = R(X + 1j*Y)\n",
"Z = R(X + 1j * Y)\n",
"plt.figure(figsize=(10, 7))\n",
"plt.contour(X, Y, np.abs(Z), levels=[1], cmap='rainbow')\n",
"plt.contour(X, Y, np.abs(Z), levels=[1], cmap=\"rainbow\")\n",
"plt.grid()\n",
"plt.show()"
]