diff --git a/L3/Analyse Matricielle/TP1_Methode_de_Gauss.ipynb b/L3/Analyse Matricielle/TP1_Methode_de_Gauss.ipynb index bb774a4..d583097 100644 --- a/L3/Analyse Matricielle/TP1_Methode_de_Gauss.ipynb +++ b/L3/Analyse Matricielle/TP1_Methode_de_Gauss.ipynb @@ -94,7 +94,7 @@ " [0, 0, 3, 0, 0],\n", " [0, 0, 0, 4, 0],\n", " [0, 0, 0, 0, 5],\n", - " ]\n", + " ],\n", ")\n", "B = np.array(\n", " [\n", @@ -103,7 +103,7 @@ " [3, 4, 5, 6, 7],\n", " [4, 5, 6, 7, 8],\n", " [5, 6, 7, 8, 9],\n", - " ]\n", + " ],\n", ")\n", "d = np.diag(A)\n", "dd = np.array([np.diag(A)])\n", @@ -299,7 +299,7 @@ " raise ValueError(\"Erreur de dimension : A doit etre carré\")\n", " if n != b.size:\n", " raise valueError(\n", - " \"Erreur de dimension : le nombre de lignes de A doit être égal au nombr ede colonnes de b\"\n", + " \"Erreur de dimension : le nombre de lignes de A doit être égal au nombr ede colonnes de b\",\n", " )\n", " U = np.zeros((n, n + 1))\n", " U = A\n", diff --git a/L3/Calculs Numériques/DM1.ipynb b/L3/Calculs Numériques/DM1.ipynb index 8db05f9..1115cec 100644 --- a/L3/Calculs Numériques/DM1.ipynb +++ b/L3/Calculs Numériques/DM1.ipynb @@ -113,9 +113,7 @@ "\n", "\n", "def C(t):\n", - " \"\"\"\n", - " Fonction retournant la solution exacte du problème au temps t\n", - " \"\"\"\n", + " \"\"\"Fonction retournant la solution exacte du problème au temps t\"\"\"\n", " return K_star + K / (1 + (K / K0 - 1) * np.exp(-r * (t - t_fl)))\n", "\n", "\n", @@ -137,9 +135,7 @@ "\n", "\n", "def dN(N, t, C_sol):\n", - " \"\"\"\n", - " Fonction calculant la dérivée de la solution approchée du problème à l'instant t dépendant de N(t) et de C(t)\n", - " \"\"\"\n", + " \"\"\"Fonction calculant la dérivée de la solution approchée du problème à l'instant t dépendant de N(t) et de C(t)\"\"\"\n", " return r_N * N * (1 - N / C_sol(t))\n", "\n", "\n", @@ -239,13 +235,19 @@ "# On crée une figure à trois graphiques\n", "fig = plt.figure(figsize=(12, 6))\n", "ax = fig.add_subplot(\n", - " 1, 2, 2\n", + " 1,\n", + " 2,\n", + " 2,\n", ") # subplot pour le champ de vecteurs et le graphe sardines vs requins\n", "axr = fig.add_subplot(\n", - " 2, 2, 1\n", + " 2,\n", + " 2,\n", + " 1,\n", ") # subplot pour le graphe du nombre de requins en fonction du temps\n", "axs = fig.add_subplot(\n", - " 2, 2, 3\n", + " 2,\n", + " 2,\n", + " 3,\n", ") # subplot pour le graphe du nombre de sardines en fonction du temps\n", "ax.quiver(sardines, requins, fsardines / n_sndmb, frequins / n_sndmb)\n", "\n", @@ -317,12 +319,10 @@ "outputs": [], "source": [ "def crank_nicolson(y0, T, N, r):\n", - " \"\"\"\n", - " schéma de Crank-Nicolson pour le modèle de Malthus\n", + " \"\"\"schéma de Crank-Nicolson pour le modèle de Malthus\n", "\n", " Parameters\n", " ----------\n", - "\n", " y0: float\n", " donnée initiale\n", " T: float\n", @@ -334,13 +334,12 @@ "\n", " Returns\n", " -------\n", - "\n", " t: ndarray\n", " les instants où la solution approchée est calculée\n", " y: ndarray\n", " les valeurs de la solution approchée par le theta-schema\n", - " \"\"\"\n", "\n", + " \"\"\"\n", " dt = T / N\n", " t = np.zeros(N + 1)\n", " y = np.zeros(N + 1)\n", @@ -357,12 +356,10 @@ "\n", "\n", "def euler_explicit(y0, T, N, r):\n", - " \"\"\"\n", - " schéma de d'Euler pour le modèle de Malthus\n", + " \"\"\"schéma de d'Euler pour le modèle de Malthus\n", "\n", " Parameters\n", " ----------\n", - "\n", " y0: float\n", " donnée initiale\n", " T: float\n", @@ -374,11 +371,11 @@ "\n", " Returns\n", " -------\n", - "\n", " t: ndarray\n", " les instants où la solution approchée est calculée\n", " y: ndarray\n", " les valeurs de la solution approchée par le theta-schema\n", + "\n", " \"\"\"\n", " dt = T / N\n", " t = np.zeros(N + 1)\n", @@ -396,9 +393,7 @@ "\n", "\n", "def solution_exacte(t):\n", - " \"\"\"\n", - " Fonction calculant la solution exacte du modèle de Malthus à l'instant t\n", - " \"\"\"\n", + " \"\"\"Fonction calculant la solution exacte du modèle de Malthus à l'instant t\"\"\"\n", " return y0 * np.exp(r * t)" ] }, @@ -462,7 +457,10 @@ "ax = fig.add_subplot(1, 2, 2)\n", "for n in liste_N:\n", " t, y = crank_nicolson(\n", - " y0, T, n, r\n", + " y0,\n", + " T,\n", + " n,\n", + " r,\n", " ) # On calcule la fonction Crank-Nicolson pour chaque n\n", " ax.scatter(t, y, label=f\"Solution approchée pour N={n}\")\n", "ax.plot(t_exact, solution_exacte(t_exact), label=\"Solution exacte\")\n", diff --git a/L3/Calculs Numériques/DM2.ipynb b/L3/Calculs Numériques/DM2.ipynb index d5f823a..4e4a187 100644 --- a/L3/Calculs Numériques/DM2.ipynb +++ b/L3/Calculs Numériques/DM2.ipynb @@ -151,20 +151,18 @@ ], "source": [ "def M(x):\n", - " \"\"\"\n", - " Retourne la matrice du système (2)\n", + " \"\"\"Retourne la matrice du système (2)\n", "\n", " Parameters\n", " ----------\n", - "\n", " x: ndarray\n", " vecteurs contenant les valeurs [x0, x1, ..., xN]\n", "\n", " Returns\n", " -------\n", - "\n", " out: ndarray\n", " matrice du système (2)\n", + "\n", " \"\"\"\n", " h = x[1:] - x[:-1] # x[i+1] - x[i]\n", " return (\n", @@ -194,12 +192,10 @@ "outputs": [], "source": [ "def sprime(x, y, p0, pN):\n", - " \"\"\"\n", - " Retourne la solution du système (2)\n", + " \"\"\"Retourne la solution du système (2)\n", "\n", " Parameters\n", " ----------\n", - "\n", " x: ndarray\n", " vecteurs contenant les valeurs [x0, x1, ..., xN]\n", " y: ndarray\n", @@ -211,9 +207,9 @@ "\n", " Returns\n", " -------\n", - "\n", " out: ndarray\n", " solution du système (2)\n", + "\n", " \"\"\"\n", " h = x[1:] - x[:-1]\n", " delta_y = (y[1:] - y[:-1]) / h\n", @@ -276,39 +272,35 @@ ], "source": [ "def f(x):\n", - " \"\"\"\n", - " Retourne la fonction f évaluée aux points x\n", + " \"\"\"Retourne la fonction f évaluée aux points x\n", "\n", " Parameters\n", " ----------\n", - "\n", " x: ndarray\n", " vecteurs contenant les valeurs [x0, x1, ..., xN]\n", "\n", " Returns\n", " -------\n", - "\n", " out: ndarray\n", " Valeur de la fonction f aux points x\n", + "\n", " \"\"\"\n", " return 1 / (1 + x**2)\n", "\n", "\n", "def fprime(x):\n", - " \"\"\"\n", - " Retourne la fonction dérivée de f évaluée aux points x\n", + " \"\"\"Retourne la fonction dérivée de f évaluée aux points x\n", "\n", " Parameters\n", " ----------\n", - "\n", " x: ndarray\n", " vecteurs contenant les valeurs [x0, x1, ..., xN]\n", "\n", " Returns\n", " -------\n", - "\n", " out: ndarray\n", " Valeur de la fonction dérivée de f aux points x\n", + "\n", " \"\"\"\n", " return -2 * x / ((1 + x**2) ** 2)\n", "\n", @@ -368,12 +360,10 @@ "outputs": [], "source": [ "def splines(x, y, p0, pN):\n", - " \"\"\"\n", - " Retourne la matrice S de taille (4, N)\n", + " \"\"\"Retourne la matrice S de taille (4, N)\n", "\n", " Parameters\n", " ----------\n", - "\n", " x: ndarray\n", " vecteurs contenant les valeurs [x0, x1, ..., xN]\n", " y: ndarray\n", @@ -385,9 +375,9 @@ "\n", " Returns\n", " -------\n", - "\n", " out: ndarray\n", " Matrice S de taille (4, N) tel que la i-ième ligne contient les valeurs a_i, b_i, c_i et d_i\n", + "\n", " \"\"\"\n", " h = x[1:] - x[:-1]\n", " delta_y = (y[1:] - y[:-1]) / h\n", @@ -420,12 +410,10 @@ "outputs": [], "source": [ "def spline_eval(x, xx, S):\n", - " \"\"\"\n", - " Evalue une spline définie par des noeuds équirepartis\n", + " \"\"\"Evalue une spline définie par des noeuds équirepartis\n", "\n", " Parameters\n", " ----------\n", - "\n", " x: ndarray\n", " noeuds définissant la spline\n", "\n", @@ -439,9 +427,9 @@ "\n", " Returns\n", " -------\n", - "\n", " ndarray\n", " ordonnées des points d'évaluation\n", + "\n", " \"\"\"\n", " ind = (np.floor((xx - x[0]) / (x[1] - x[0]))).astype(int)\n", " ind = np.where(ind == x.size - 1, ind - 1, ind)\n", diff --git a/L3/Calculs Numériques/DM3.ipynb b/L3/Calculs Numériques/DM3.ipynb index abfcf6a..fe95e66 100644 --- a/L3/Calculs Numériques/DM3.ipynb +++ b/L3/Calculs Numériques/DM3.ipynb @@ -180,11 +180,11 @@ "source": [ "for f in [f0, f1, f2, f3]:\n", " print(\n", - " f\"Calcule de I(f) par la méthode de gauss et par la formule quadratique pour la fonction {f.__name__}\"\n", + " f\"Calcule de I(f) par la méthode de gauss et par la formule quadratique pour la fonction {f.__name__}\",\n", " )\n", " for n in range(1, 11):\n", " print(f\"Pour n = {n}, gauss = {gauss(f, n)} et quad = {quad(f, -1, 1)[0]}\")\n", - " print(\"\")" + " print()" ] }, { @@ -276,11 +276,11 @@ "source": [ "for f in [f0, f1, f2, f3]:\n", " print(\n", - " f\"Calcule de I(f) par la méthode de simpson et par la formule quadratique pour la fonction {f.__name__}\"\n", + " f\"Calcule de I(f) par la méthode de simpson et par la formule quadratique pour la fonction {f.__name__}\",\n", " )\n", " for n in range(3, 16, 2):\n", " print(f\"Pour n = {n}, simpson = {simpson(f, n)} et quad = {quad(f, -1, 1)[0]}\")\n", - " print(\"\")" + " print()" ] }, { @@ -340,10 +340,9 @@ "def poly_tchebychev(x, N):\n", " if N == 0:\n", " return np.ones_like(x)\n", - " elif N == 1:\n", + " if N == 1:\n", " return x\n", - " else:\n", - " return 2 * x * poly_tchebychev(x, N - 1) - poly_tchebychev(x, N - 2)" + " return 2 * x * poly_tchebychev(x, N - 1) - poly_tchebychev(x, N - 2)" ] }, { @@ -421,7 +420,7 @@ " print(f\"Pour N = {n}\")\n", " print(f\"Les points de Tchebychev sont {xk}\")\n", " print(f\"L'evaluation du polynome de Tchebychev Tn en ces points est {Tn}\")\n", - " print(\"\")" + " print()" ] }, { @@ -537,11 +536,11 @@ "source": [ "for f in [f0, f1, f2, f3]:\n", " print(\n", - " f\"Calcule de I(f) par la méthode de fejer et par la formule quadratique pour la fonction {f.__name__}\"\n", + " f\"Calcule de I(f) par la méthode de fejer et par la formule quadratique pour la fonction {f.__name__}\",\n", " )\n", " for n in range(1, 11):\n", " print(f\"Pour n = {n}, fejer = {fejer(f, n)} et quad = {quad(f, -1, 1)[0]}\")\n", - " print(\"\")" + " print()" ] }, { @@ -603,7 +602,10 @@ " marker=\"+\",\n", " )\n", " ax.scatter(\n", - " np.arange(3, N + 1, 2), np.log10(error_simp), label=\"Simpson\", marker=\"+\"\n", + " np.arange(3, N + 1, 2),\n", + " np.log10(error_simp),\n", + " label=\"Simpson\",\n", + " marker=\"+\",\n", " )\n", " ax.scatter(\n", " np.arange(1, N + 1),\n", @@ -703,8 +705,14 @@ "print(\"-----------------------------------------------------------------------\")\n", "print(\n", " \"{:>5s} | {:>7s} {:>9s} {:>9s} {:>9s} {:>9s} {:>9s}\".format(\n", - " \"N\", \"x^0\", \"x^2\", \"x^4\", \"x^6\", \"x^8\", \"x^10\"\n", - " )\n", + " \"N\",\n", + " \"x^0\",\n", + " \"x^2\",\n", + " \"x^4\",\n", + " \"x^6\",\n", + " \"x^8\",\n", + " \"x^10\",\n", + " ),\n", ")\n", "print(\"-----------------------------------------------------------------------\")\n", "\n", @@ -758,8 +766,14 @@ "print(\"-----------------------------------------------------------------------\")\n", "print(\n", " \"{:>5s} | {:>7s} {:>9s} {:>9s} {:>9s} {:>9s} {:>9s}\".format(\n", - " \"N\", \"x^0\", \"x^2\", \"x^4\", \"x^6\", \"x^8\", \"x^10\"\n", - " )\n", + " \"N\",\n", + " \"x^0\",\n", + " \"x^2\",\n", + " \"x^4\",\n", + " \"x^6\",\n", + " \"x^8\",\n", + " \"x^10\",\n", + " ),\n", ")\n", "print(\"-----------------------------------------------------------------------\")\n", "\n", diff --git a/L3/Calculs Numériques/Interpolation.ipynb b/L3/Calculs Numériques/Interpolation.ipynb index d5405ec..6c5e224 100644 --- a/L3/Calculs Numériques/Interpolation.ipynb +++ b/L3/Calculs Numériques/Interpolation.ipynb @@ -527,7 +527,7 @@ " \"\"\"Build the Vandermonde matrix for points x.\"\"\"\n", " size = x.shape[0]\n", " M = np.reshape(x, (size, 1))\n", - " power = list(range(0, size))\n", + " power = list(range(size))\n", " return M**power\n", "\n", "\n", diff --git a/L3/Calculs Numériques/Interpolation_2.ipynb b/L3/Calculs Numériques/Interpolation_2.ipynb index 5989aad..a8107ea 100644 --- a/L3/Calculs Numériques/Interpolation_2.ipynb +++ b/L3/Calculs Numériques/Interpolation_2.ipynb @@ -163,7 +163,7 @@ " [\n", " (a + b) / 2 + (b - a) / 2 * np.cos((2 * i - 1) / (2 * N) * np.pi)\n", " for i in range(1, N + 1)\n", - " ]\n", + " ],\n", " )" ] }, diff --git a/L3/Calculs Numériques/Intégration.ipynb b/L3/Calculs Numériques/Intégration.ipynb index fca8385..313e557 100644 --- a/L3/Calculs Numériques/Intégration.ipynb +++ b/L3/Calculs Numériques/Intégration.ipynb @@ -151,11 +151,9 @@ " return np.ones_like(x)\n", " if n == 1:\n", " return x\n", - " else:\n", - " return (\n", - " (2 * n - 1) * x * poly_legendre(x, n - 1)\n", - " - (n - 1) * poly_legendre(x, n - 2)\n", - " ) / n" + " return (\n", + " (2 * n - 1) * x * poly_legendre(x, n - 1) - (n - 1) * poly_legendre(x, n - 2)\n", + " ) / n" ] }, { @@ -509,7 +507,7 @@ " for f in [f0, f1]:\n", " print(f\"I({f.__name__}) par quad_gauss: {quad_gauss(f, n)}\")\n", " print(f\"I({f.__name__}) par quad: {quad(f, -1, 1)[0]}\")\n", - " print(\"\")" + " print()" ] }, { diff --git a/L3/Calculs Numériques/Methode_de_Newton.ipynb b/L3/Calculs Numériques/Methode_de_Newton.ipynb index b8cb0df..432113c 100644 --- a/L3/Calculs Numériques/Methode_de_Newton.ipynb +++ b/L3/Calculs Numériques/Methode_de_Newton.ipynb @@ -107,7 +107,7 @@ "\n", "def f1prime(x):\n", " \"\"\"Return the value of the derivative of f1 at point x.\"\"\"\n", - " return 2 * np.cos(x) * np.sin(x)\n" + " return 2 * np.cos(x) * np.sin(x)" ] }, { @@ -463,7 +463,7 @@ " for n in range(1, 21):\n", " x_sol = newton(f, fprime, x_start, epsilon, n)\n", " print(f\"x_sol = {x_sol[1]} pour {x_sol[0]} itérations\")\n", - " print(\"\")\n", + " print()\n", "\n", "xx = np.linspace(-3, 3, 200)\n", "\n", @@ -526,7 +526,7 @@ "\n", "# DECOMMENTEZ\n", "img0 = mpimg.imread(\"image0.png\")\n", - "plt.imshow(img0)\n" + "plt.imshow(img0)" ] }, { @@ -757,7 +757,7 @@ "\n", "\n", "roots = np.array(\n", - " [complex(-1 / 2, np.sqrt(3) / 2), complex(-1 / 2, -np.sqrt(3) / 2), complex(1, 0)]\n", + " [complex(-1 / 2, np.sqrt(3) / 2), complex(-1 / 2, -np.sqrt(3) / 2), complex(1, 0)],\n", ")" ] }, diff --git a/L3/Calculs Numériques/Point_Fixe.ipynb b/L3/Calculs Numériques/Point_Fixe.ipynb index 4772663..4463989 100644 --- a/L3/Calculs Numériques/Point_Fixe.ipynb +++ b/L3/Calculs Numériques/Point_Fixe.ipynb @@ -143,12 +143,10 @@ "outputs": [], "source": [ "def point_fixe(f, x0, tol=1.0e-6, itermax=5000):\n", - " \"\"\"\n", - " Recherche de point fixe : méthode brute x_{n+1} = f(x_n)\n", + " \"\"\"Recherche de point fixe : méthode brute x_{n+1} = f(x_n)\n", "\n", " Parameters\n", " ----------\n", - "\n", " f: function\n", " la fonction dont on cherche le point fixe\n", " x0: float\n", @@ -160,13 +158,13 @@ "\n", " Returns\n", " -------\n", - "\n", " x: float\n", " la valeur trouvée pour le point fixe\n", " niter: int\n", " le nombre d'itérations effectuées\n", " xL: ndarray\n", " la suite des itérés de la suite\n", + "\n", " \"\"\"\n", " xL = [x0]\n", " niter = 0\n", diff --git a/L3/Equations Différentielles/TP1_EDO_EulerExp.ipynb b/L3/Equations Différentielles/TP1_EDO_EulerExp.ipynb index 97bfb0a..b112b3b 100644 --- a/L3/Equations Différentielles/TP1_EDO_EulerExp.ipynb +++ b/L3/Equations Différentielles/TP1_EDO_EulerExp.ipynb @@ -135,10 +135,10 @@ "y = np.linspace(0, 2.1, 23) # ordonnées des points de la grille\n", "T, Y = np.meshgrid(t, y) # grille de points dans le plan (t,y)\n", "U = np.ones(T.shape) / np.sqrt(\n", - " 1 + f1(T, Y) ** 2\n", + " 1 + f1(T, Y) ** 2,\n", ") # matrice avec les composantes horizontales des vecteurs (1), normalisées\n", "V = f1(T, Y) / np.sqrt(\n", - " 1 + f1(T, Y) ** 2\n", + " 1 + f1(T, Y) ** 2,\n", ") # matrice avec les composantes verticales des vecteurs (f(t,y)), normalisées\n", "plt.quiver(T, Y, U, V, angles=\"xy\", scale=20, color=\"cyan\")\n", "plt.axis([-5, 5, 0, 2.1])" @@ -227,10 +227,10 @@ "y = np.linspace(ymin, ymax) # ordonnées des points de la grille\n", "T, Y = np.meshgrid(t, y) # grille de points dans le plan (t,y)\n", "U = np.ones(T.shape) / np.sqrt(\n", - " 1 + f2(T, Y) ** 2\n", + " 1 + f2(T, Y) ** 2,\n", ") # matrice avec les composantes horizontales des vecteurs (1), normalisées\n", "V = f1(T, Y) / np.sqrt(\n", - " 1 + f2(T, Y) ** 2\n", + " 1 + f2(T, Y) ** 2,\n", ") # matrice avec les composantes verticales des vecteurs (f(t,y)), normalisées\n", "plt.quiver(T, Y, U, V, angles=\"xy\", scale=20, color=\"cyan\")\n", "plt.axis([xmin, xmax, ymin, ymax])" @@ -484,10 +484,10 @@ "y = np.linspace(0, K + 100, 23) # ordonnées des points de la grille\n", "T, P = np.meshgrid(t, y) # grille de points dans le plan (t,y)\n", "U = np.ones(T.shape) / np.sqrt(\n", - " 1 + fV(T, P) ** 2\n", + " 1 + fV(T, P) ** 2,\n", ") # matrice avec les composantes horizontales des vecteurs (1), normalisées\n", "V = fV(T, P) / np.sqrt(\n", - " 1 + fV(T, P) ** 2\n", + " 1 + fV(T, P) ** 2,\n", ") # matrice avec les composantes verticales des vecteurs (f(t,y)), normalisées\n", "plt.quiver(T, P, U, V, angles=\"xy\", scale=20, color=\"cyan\")\n", "plt.legend(fontsize=4)" @@ -570,10 +570,10 @@ "y = np.linspace(0, 6, 23) # ordonnées des points de la grille\n", "T, P = np.meshgrid(t, y) # grille de points dans le plan (t,y)\n", "U = np.ones(T.shape) / np.sqrt(\n", - " 1 + fS(T, P) ** 2\n", + " 1 + fS(T, P) ** 2,\n", ") # matrice avec les composantes horizontales des vecteurs (1), normalisées\n", "V = fS(T, P) / np.sqrt(\n", - " 1 + fS(T, P) ** 2\n", + " 1 + fS(T, P) ** 2,\n", ") # matrice avec les composantes verticales des vecteurs (f(t,y)), normalisées\n", "plt.quiver(T, P, U, V, angles=\"xy\", scale=20, color=\"cyan\")" ] diff --git a/L3/Equations Différentielles/TP2_Lokta_Volterra.ipynb b/L3/Equations Différentielles/TP2_Lokta_Volterra.ipynb index 5321d00..72d1faa 100644 --- a/L3/Equations Différentielles/TP2_Lokta_Volterra.ipynb +++ b/L3/Equations Différentielles/TP2_Lokta_Volterra.ipynb @@ -125,7 +125,7 @@ " [\n", " (C[0] * np.exp(-(t - t0)) * U1[0] + C[1] * np.exp(-2 * (t - t0)) * U2[0]),\n", " (C[0] * np.exp(-(t - t0)) * U1[1] + C[1] * np.exp(-2 * (t - t0)) * U2[1]),\n", - " ]\n", + " ],\n", " )\n", "\n", "\n", diff --git a/L3/Equations Différentielles/TP3_Convergence.ipynb b/L3/Equations Différentielles/TP3_Convergence.ipynb index 637a2c9..db6f13d 100644 --- a/L3/Equations Différentielles/TP3_Convergence.ipynb +++ b/L3/Equations Différentielles/TP3_Convergence.ipynb @@ -224,7 +224,7 @@ "plt.xlabel(\"$t$\")\n", "plt.ylabel(\"$y^n$\")\n", "plt.title(\n", - " \"Solutions approchées de (P) obtenus avec mon_schema pour différentes valeurs du pas h\"\n", + " \"Solutions approchées de (P) obtenus avec mon_schema pour différentes valeurs du pas h\",\n", ")" ] }, @@ -274,7 +274,7 @@ "plt.xlabel(\"$t$\")\n", "plt.ylabel(\"$|y(t_n) - y^n|$\")\n", "plt.title(\n", - " \"différence en valeur absolue entre sol. exacte et sol. approchée par mon_schema, pour différentes valeurs du pas h\"\n", + " \"différence en valeur absolue entre sol. exacte et sol. approchée par mon_schema, pour différentes valeurs du pas h\",\n", ")" ] }, @@ -357,7 +357,7 @@ "\n", "plt.legend()\n", "plt.title(\n", - " \"Erreur pour la méthode mon_schema en echelle logarithmique : log(E) en fonction de log(h)\"\n", + " \"Erreur pour la méthode mon_schema en echelle logarithmique : log(E) en fonction de log(h)\",\n", ")\n", "plt.xlabel(\"$log(h)$\")\n", "plt.ylabel(\"$log(E)$\")" @@ -674,7 +674,7 @@ "plt.xlabel(\"$t$\")\n", "plt.ylabel(\"$|y(t_n) - y^n|$\")\n", "plt.title(\n", - " \"différence en valeur absolue entre sol. exacte et sol. approchée, pour différents schemas\"\n", + " \"différence en valeur absolue entre sol. exacte et sol. approchée, pour différents schemas\",\n", ")" ] }, @@ -826,7 +826,7 @@ " plt.xlabel(\"$t$\")\n", " plt.ylabel(\"$y^n$\")\n", " plt.title(\n", - " f\"Solutions approchées de (P) obtenus avec {schema.__name__} pour différentes valeurs du pas h\"\n", + " f\"Solutions approchées de (P) obtenus avec {schema.__name__} pour différentes valeurs du pas h\",\n", " )" ] }, @@ -886,7 +886,7 @@ "\n", " plt.legend()\n", " plt.title(\n", - " f\"Erreur pour la méthode {schema.__name__} en echelle logarithmique : log(E) en fonction de log(h)\"\n", + " f\"Erreur pour la méthode {schema.__name__} en echelle logarithmique : log(E) en fonction de log(h)\",\n", " )\n", " plt.xlabel(\"$log(h)$\")\n", " plt.ylabel(\"$log(E)$\")" diff --git a/L3/Méthodes Numériques/TP1_Equation_de_Poisson.ipynb b/L3/Méthodes Numériques/TP1_Equation_de_Poisson.ipynb index c3106b4..ba11d38 100644 --- a/L3/Méthodes Numériques/TP1_Equation_de_Poisson.ipynb +++ b/L3/Méthodes Numériques/TP1_Equation_de_Poisson.ipynb @@ -361,7 +361,7 @@ "plt.ylabel(r\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n", "plt.legend(fontsize=7)\n", "plt.title(\n", - " \"Différence en valeur absolue entre la solution exacte et la solution approchée\"\n", + " \"Différence en valeur absolue entre la solution exacte et la solution approchée\",\n", ")" ] }, @@ -440,7 +440,7 @@ "plt.xlabel(\"$log(h)$\")\n", "plt.ylabel(\"$log(E)$\")\n", "plt.title(\n", - " \"Erreur pour la méthode mon_schema en echelle logarithmique : log(E) en fonction de log(h)\"\n", + " \"Erreur pour la méthode mon_schema en echelle logarithmique : log(E) en fonction de log(h)\",\n", ")" ] }, @@ -592,7 +592,7 @@ "plt.ylabel(r\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n", "plt.legend(fontsize=7)\n", "plt.title(\n", - " \"Différence en valeur absolue entre la solution exacte et la solution approchée\"\n", + " \"Différence en valeur absolue entre la solution exacte et la solution approchée\",\n", ")" ] }, @@ -672,7 +672,7 @@ "plt.xlabel(\"$log(h)$\")\n", "plt.ylabel(\"$log(E)$\")\n", "plt.title(\n", - " \"Erreur pour la méthode mon_schema en echelle logarithmique : log(E) en fonction de log(h)\"\n", + " \"Erreur pour la méthode mon_schema en echelle logarithmique : log(E) en fonction de log(h)\",\n", ")" ] }, @@ -781,7 +781,11 @@ " x, U_app = solution_neumann(f3, M, a, b)\n", "\n", " plt.scatter(\n", - " x, U_app, marker=\"+\", s=3, label=\"$h^2({A_N}_h + I_M)U = h^2F$ pour M={M}\"\n", + " x,\n", + " U_app,\n", + " marker=\"+\",\n", + " s=3,\n", + " label=\"$h^2({A_N}_h + I_M)U = h^2F$ pour M={M}\",\n", " )\n", "plt.plot(x, u3(x), label=\"Solution exacte\", color=\"red\")\n", "plt.legend(fontsize=8)\n", @@ -836,7 +840,7 @@ "plt.ylabel(r\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n", "plt.legend(fontsize=7)\n", "plt.title(\n", - " \"Différence en valeur absolue entre la solution exacte et la solution approchée\"\n", + " \"Différence en valeur absolue entre la solution exacte et la solution approchée\",\n", ")" ] }, @@ -877,7 +881,7 @@ "plt.xlabel(\"$log(h)$\")\n", "plt.ylabel(\"$log(E)$\")\n", "plt.title(\n", - " \"Erreur pour la méthode mon_schema en echelle logarithmique : log(E) en fonction de log(h)\"\n", + " \"Erreur pour la méthode mon_schema en echelle logarithmique : log(E) en fonction de log(h)\",\n", ")" ] }, diff --git a/L3/Projet Numérique/Segregation.ipynb b/L3/Projet Numérique/Segregation.ipynb index 625489b..1c92e04 100644 --- a/L3/Projet Numérique/Segregation.ipynb +++ b/L3/Projet Numérique/Segregation.ipynb @@ -51,7 +51,9 @@ " def initialisation_grille(self):\n", " grille = np.zeros((self.M, self.M), dtype=int)\n", " occupes = np.random.choice(\n", - " self.M * self.M, size=int((1 - self.p) * self.M * self.M), replace=False\n", + " self.M * self.M,\n", + " size=int((1 - self.p) * self.M * self.M),\n", + " replace=False,\n", " )\n", " self.Ntot = len(occupes)\n", "\n", @@ -82,19 +84,14 @@ " case = self.grille[i, j]\n", " if case == 0:\n", " pass\n", + " elif case == groupe:\n", + " count_similaires += 1\n", " else:\n", - " if case == groupe:\n", - " count_similaires += 1\n", - " else:\n", - " count_differents += 1\n", + " count_differents += 1\n", "\n", " if count_similaires + count_differents == 0:\n", " return False\n", - " else:\n", - " return (\n", - " float(count_similaires / (count_similaires + count_differents))\n", - " >= self.L\n", - " )\n", + " return float(count_similaires / (count_similaires + count_differents)) >= self.L\n", "\n", " def clusters(self):\n", " visited = np.zeros_like(self.grille, dtype=bool)\n", @@ -179,7 +176,7 @@ " self.grille[agent[0]][agent[1]] = 0\n", " cases_non_occupees.append(agent)\n", " self.afficher_grille(\n", - " f\"Configuration Finale de T={T} pour (M, p, L) = ({self.M},{self.p},{self.L})\"\n", + " f\"Configuration Finale de T={T} pour (M, p, L) = ({self.M},{self.p},{self.L})\",\n", " )" ] }, @@ -744,7 +741,7 @@ " plt.xlabel(\"L\")\n", " plt.ylabel(\"S\")\n", " plt.title(\n", - " f\"Evolution du coefficient de ségrégation S en fonction de la tolérance L = {L} pour p = {p}\"\n", + " f\"Evolution du coefficient de ségrégation S en fonction de la tolérance L = {L} pour p = {p}\",\n", " )" ] }, diff --git a/L3/Statistiques/TP1.ipynb b/L3/Statistiques/TP1.ipynb index 72b2df9..9da4998 100644 --- a/L3/Statistiques/TP1.ipynb +++ b/L3/Statistiques/TP1.ipynb @@ -11,7 +11,7 @@ "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", - "import scipy.stats as stats" + "from scipy import stats" ] }, { @@ -144,7 +144,11 @@ " label=\"Echantillon de X - Y/2\",\n", ")\n", "plt.hist(\n", - " sampleZ / 2, bins=intervalle, density=True, alpha=0.7, label=\"Echantillon de Z\"\n", + " sampleZ / 2,\n", + " bins=intervalle,\n", + " density=True,\n", + " alpha=0.7,\n", + " label=\"Echantillon de Z\",\n", ")\n", "plt.legend()" ] @@ -178,7 +182,10 @@ " sample = np.random.binomial(k, p, nb_repl)\n", " intervalle = np.linspace(np.min(sample), np.max(sample), 100)\n", " plt.hist(\n", - " sample, bins=intervalle, density=True, label=f\"Echantillon de X pour n={k}\"\n", + " sample,\n", + " bins=intervalle,\n", + " density=True,\n", + " label=f\"Echantillon de X pour n={k}\",\n", " )\n", " plt.legend()" ] @@ -271,7 +278,7 @@ "\n", "for _ in range(nb_lgn):\n", " liste_Sn.append(\n", - " np.mean(np.sqrt(3 * nb_repl) * np.tan(np.pi / 2 * sample_uniforme(nb_repl)))\n", + " np.mean(np.sqrt(3 * nb_repl) * np.tan(np.pi / 2 * sample_uniforme(nb_repl))),\n", " )\n", "\n", "# nb_bins = 100\n", diff --git a/L3/Statistiques/TP2.ipynb b/L3/Statistiques/TP2.ipynb index 4330667..1bf8248 100644 --- a/L3/Statistiques/TP2.ipynb +++ b/L3/Statistiques/TP2.ipynb @@ -13,7 +13,7 @@ "import numpy as np\n", "import scipy.optimize as opt\n", "import scipy.special as sp\n", - "import scipy.stats as stats" + "from scipy import stats" ] }, { diff --git a/M1/Numerical Methods/TP1.ipynb b/M1/Numerical Methods/TP1.ipynb index 1cb6891..e6a74fd 100644 --- a/M1/Numerical Methods/TP1.ipynb +++ b/M1/Numerical Methods/TP1.ipynb @@ -338,7 +338,7 @@ " return np.max(\n", " np.power(np.abs(sol_appr - sol_exact), 2)[\n", " np.isfinite(np.power(np.abs(sol_appr - sol_exact), 2))\n", - " ]\n", + " ],\n", " )\n", "\n", "\n", diff --git a/M1/Numerical Methods/TP2.ipynb b/M1/Numerical Methods/TP2.ipynb index d28d7b1..d446487 100644 --- a/M1/Numerical Methods/TP2.ipynb +++ b/M1/Numerical Methods/TP2.ipynb @@ -38,6 +38,7 @@ ], "source": [ "import numpy as np\n", + "\n", "from sklearn.datasets import make_classification\n", "from sklearn.metrics import accuracy_score\n", "from sklearn.model_selection import train_test_split\n", @@ -47,12 +48,18 @@ "\n", "for _ in range(10):\n", " X, y = make_classification(\n", - " n_samples=1000, n_features=4, n_classes=3, n_clusters_per_class=1\n", + " n_samples=1000,\n", + " n_features=4,\n", + " n_classes=3,\n", + " n_clusters_per_class=1,\n", " )\n", "\n", " X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n", " model = MLPClassifier(\n", - " hidden_layer_sizes=(5, 7), activation=\"relu\", max_iter=10000, solver=\"adam\"\n", + " hidden_layer_sizes=(5, 7),\n", + " activation=\"relu\",\n", + " max_iter=10000,\n", + " solver=\"adam\",\n", " )\n", " model.fit(X_train, y_train)\n", "\n", diff --git a/M1/Numerical Methods/TP2_DANJOU_Arthur.ipynb b/M1/Numerical Methods/TP2_DANJOU_Arthur.ipynb index 0b5addf..7edc7b3 100644 --- a/M1/Numerical Methods/TP2_DANJOU_Arthur.ipynb +++ b/M1/Numerical Methods/TP2_DANJOU_Arthur.ipynb @@ -17,7 +17,7 @@ "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", - "import scipy.stats as stats" + "from scipy import stats" ] }, { @@ -46,15 +46,12 @@ "outputs": [], "source": [ "def S(t, S0, mu, sigma, W):\n", - " \"\"\"\n", - " Solution exacte de l'EDS de Black-Scholes\n", - " \"\"\"\n", + " \"\"\"Solution exacte de l'EDS de Black-Scholes\"\"\"\n", " return S0 * np.exp((mu - 0.5 * sigma**2) * t + sigma * W)\n", "\n", "\n", "def euler_maruyama(mu, sigma, T, N, X0=0.0):\n", - " \"\"\"\n", - " Simulation d'une EDS de Black-Scholes par la méthode d'Euler-Maruyama\n", + " \"\"\"Simulation d'une EDS de Black-Scholes par la méthode d'Euler-Maruyama\n", "\n", " Paramètres :\n", " mu (float) : drift\n", @@ -84,8 +81,7 @@ "\n", "\n", "def plot_brownien(t, X, B=None):\n", - " \"\"\"\n", - " Plot la simulation d'Euler-Maruyama\n", + " \"\"\"Plot la simulation d'Euler-Maruyama\n", "\n", " Paramètres :\n", " t (array-like) : tableau des temps\n", @@ -169,8 +165,7 @@ "\n", "\n", "def plot_convergence(S0, mu, sigma, T):\n", - " \"\"\"\n", - " Plot la convergence du schéma d'Euler-Maruyama\n", + " \"\"\"Plot la convergence du schéma d'Euler-Maruyama\n", "\n", " Paramètres :\n", " S0 (int) : valeur initiale\n", @@ -291,7 +286,7 @@ "print(\n", " \"La barrière a été franchie\"\n", " if is_barrier_breached(X, B)\n", - " else \"La barrière n'a pas été franchie\"\n", + " else \"La barrière n'a pas été franchie\",\n", ")" ] }, @@ -335,8 +330,7 @@ " \"\"\"\n", " if not is_barrier_breached(X, B):\n", " return max(X[-1] - K, 0)\n", - " else:\n", - " return 0\n", + " return 0\n", "\n", "\n", "def call_BS(x):\n", diff --git a/M1/Numerical Methods/TP_DANJOU_Arthur.ipynb b/M1/Numerical Methods/TP_DANJOU_Arthur.ipynb index 55b131b..17ed746 100644 --- a/M1/Numerical Methods/TP_DANJOU_Arthur.ipynb +++ b/M1/Numerical Methods/TP_DANJOU_Arthur.ipynb @@ -51,7 +51,7 @@ " for h in h_list:\n", " t = np.arange(a, b, h)\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", + " [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", @@ -326,7 +326,7 @@ " 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", "\n", diff --git a/M1/Numerical Optimisation/ComputerSession1.ipynb b/M1/Numerical Optimisation/ComputerSession1.ipynb index b07ddbd..5bfe864 100644 --- a/M1/Numerical Optimisation/ComputerSession1.ipynb +++ b/M1/Numerical Optimisation/ComputerSession1.ipynb @@ -682,7 +682,7 @@ " [\n", " (F(x + delta * e(i, d)) - F(x - delta * e(i, d))) / (2 * delta)\n", " for i in range(d)\n", - " ]\n", + " ],\n", " )\n", "\n", "\n", diff --git a/M1/Numerical Optimisation/ComputerSession2.ipynb b/M1/Numerical Optimisation/ComputerSession2.ipynb index 365d0fb..0d80310 100644 --- a/M1/Numerical Optimisation/ComputerSession2.ipynb +++ b/M1/Numerical Optimisation/ComputerSession2.ipynb @@ -384,7 +384,7 @@ "optimal_point_newton, iterations_newton = newton_method(initial_guess_newton)\n", "print(f\"Optimal point (Newton): {optimal_point_newton}\")\n", "print(\n", - " f\"Objective function value at optimal point (Newton): {objective_function(optimal_point_newton)}\"\n", + " f\"Objective function value at optimal point (Newton): {objective_function(optimal_point_newton)}\",\n", ")\n", "print(f\"Number of iterations (Newton): {iterations_newton}\")\n", "\n", @@ -395,7 +395,7 @@ "optimal_point_dichotomy, iterations_dichotomy = dichotomy_method(aL, aR)\n", "print(f\"Optimal point (Dichotomy): {optimal_point_dichotomy}\")\n", "print(\n", - " f\"Objective function value at optimal point (Dichotomy): {objective_function(optimal_point_dichotomy)}\"\n", + " f\"Objective function value at optimal point (Dichotomy): {objective_function(optimal_point_dichotomy)}\",\n", ")\n", "print(f\"Number of iterations (Dichotomy): {iterations_dichotomy}\")" ] diff --git a/M1/Numerical Optimisation/ComputerSession3.ipynb b/M1/Numerical Optimisation/ComputerSession3.ipynb index 5471467..704ef34 100644 --- a/M1/Numerical Optimisation/ComputerSession3.ipynb +++ b/M1/Numerical Optimisation/ComputerSession3.ipynb @@ -46,7 +46,7 @@ "def generate_thetas(n):\n", " random_steps = np.random.random(n)\n", " return np.concatenate(\n", - " ([0], np.cumsum(random_steps / np.sum(random_steps) * (2 * np.pi)))\n", + " ([0], np.cumsum(random_steps / np.sum(random_steps) * (2 * np.pi))),\n", " )\n", "\n", "\n", diff --git a/M1/Portfolio Management/TP-Project.ipynb b/M1/Portfolio Management/TP-Project.ipynb index e01e3be..39df4b0 100644 --- a/M1/Portfolio Management/TP-Project.ipynb +++ b/M1/Portfolio Management/TP-Project.ipynb @@ -27,9 +27,10 @@ }, "outputs": [], "source": [ + "import yfinance as yf\n", + "\n", "import numpy as np\n", - "import pandas as pd\n", - "import yfinance as yf" + "import pandas as pd" ] }, { @@ -406,7 +407,7 @@ "print(f\"Standard deviation sd_T: {sd_T}\")\n", "print(f\"Allocation pi_T: {pi_T}\")\n", "print(\n", - " f\"We can verify that the allocation is possible as the sum of the allocations for the different indices is {sum(pi_T)}, that is very close to 1\"\n", + " f\"We can verify that the allocation is possible as the sum of the allocations for the different indices is {sum(pi_T)}, that is very close to 1\",\n", ")" ] }, @@ -452,9 +453,9 @@ "for i in range(len(std)):\n", " print(f\"The annualized volatilities of the index {Tickers[i]} is {std[i]}\")\n", " print(\n", - " f\"The annualized expected returns of the index {Tickers[i]} is {mean[Tickers[i]]}\"\n", + " f\"The annualized expected returns of the index {Tickers[i]} is {mean[Tickers[i]]}\",\n", " )\n", - " print(\"\")\n", + " print()\n", "\n", "print(f\"The annualized volatility of the Tangent Portfolio is {sd_T * np.sqrt(252)}\")\n", "print(f\"The annualized expected return of the Tangent Portfolio is {m_T * 252}\")" @@ -494,7 +495,7 @@ "\n", "for i in range(4):\n", " print(\n", - " f\"the sharpe ratio of the index {Tickers[i]} is {(mean[Tickers[i]] - r) / std[i]}\"\n", + " f\"the sharpe ratio of the index {Tickers[i]} is {(mean[Tickers[i]] - r) / std[i]}\",\n", " )" ] } diff --git a/M1/Portfolio Management/TP1.ipynb b/M1/Portfolio Management/TP1.ipynb index b844ac3..2ed98db 100644 --- a/M1/Portfolio Management/TP1.ipynb +++ b/M1/Portfolio Management/TP1.ipynb @@ -13,9 +13,10 @@ }, "outputs": [], "source": [ + "import yfinance as yf\n", + "\n", "import numpy as np\n", - "import pandas as pd\n", - "import yfinance as yf" + "import pandas as pd" ] }, { @@ -530,7 +531,7 @@ "\n", "# Self financing portfolio\n", "m_w = np.sqrt(\n", - " (mean - b / a * vec1).T.dot(inv_sigma).dot(mean - b / a * vec1)\n", + " (mean - b / a * vec1).T.dot(inv_sigma).dot(mean - b / a * vec1),\n", ") # Expected return\n", "\n", "# Tangent portfolio\n", @@ -580,7 +581,7 @@ "range_sup = np.max(mean) + 1\n", "y = np.linspace(range_inf, range_sup, 50)\n", "x_1 = np.array(\n", - " [np.sqrt(((y - m_a) / m_w) ** 2 + sd_a**2)]\n", + " [np.sqrt(((y - m_a) / m_w) ** 2 + sd_a**2)],\n", ") # Sigma values for the frontier\n", "x_2 = np.array([(y - r) / (m_T - r) * sd_T]) # Sigma values for the Capital Market Line\n", "\n", diff --git a/M1/Statistical Learning/TP0_Intro_Jupyter_Python.ipynb b/M1/Statistical Learning/TP0_Intro_Jupyter_Python.ipynb index 5fd2c76..0cd2e2f 100644 --- a/M1/Statistical Learning/TP0_Intro_Jupyter_Python.ipynb +++ b/M1/Statistical Learning/TP0_Intro_Jupyter_Python.ipynb @@ -1114,7 +1114,12 @@ "R = multivariate_normal([0, 0], np.eye(2))\n", "\n", "surf = ax.plot_surface(\n", - " X, Y, R.pdf(np.dstack((X, Y))), cmap=\"coolwarm\", linewidth=0, antialiased=False\n", + " X,\n", + " Y,\n", + " R.pdf(np.dstack((X, Y))),\n", + " cmap=\"coolwarm\",\n", + " linewidth=0,\n", + " antialiased=False,\n", ")\n", "\n", "fig.colorbar(surf, shrink=0.5, aspect=5)\n", diff --git a/M1/Statistical Learning/TP2_KNN.ipynb b/M1/Statistical Learning/TP2_KNN.ipynb index ea6653e..5f529c6 100644 --- a/M1/Statistical Learning/TP2_KNN.ipynb +++ b/M1/Statistical Learning/TP2_KNN.ipynb @@ -238,7 +238,10 @@ "from sklearn.model_selection import train_test_split\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(\n", - " X, y, test_size=0.33, random_state=42\n", + " X,\n", + " y,\n", + " test_size=0.33,\n", + " random_state=42,\n", ")" ] }, @@ -702,10 +705,10 @@ "predictions2 = [knn_class_2(X_train, y_train, data, 3) for data in X_test]\n", "\n", "print(\n", - " f\"The accuracy rate of our classifier is {np.sum(predictions == y_test) / len(predictions) * 100}%\"\n", + " f\"The accuracy rate of our classifier is {np.sum(predictions == y_test) / len(predictions) * 100}%\",\n", ")\n", "print(\n", - " f\"The accuracy rate of our classifier is {np.sum(predictions2 == y_test) / len(predictions2) * 100}%\"\n", + " f\"The accuracy rate of our classifier is {np.sum(predictions2 == y_test) / len(predictions2) * 100}%\",\n", ")" ] }, @@ -1278,6 +1281,7 @@ "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", + "\n", "from sklearn.neighbors import KNeighborsClassifier" ] }, @@ -2094,7 +2098,10 @@ "outputs": [], "source": [ "X_train, X_test, y_train, y_test = train_test_split(\n", - " X, y, test_size=0.33, random_state=42\n", + " X,\n", + " y,\n", + " test_size=0.33,\n", + " random_state=42,\n", ")" ] }, diff --git a/M1/Statistical Learning/TP2_bis_OPTIONAL.ipynb b/M1/Statistical Learning/TP2_bis_OPTIONAL.ipynb index 170dcca..cb62dda 100644 --- a/M1/Statistical Learning/TP2_bis_OPTIONAL.ipynb +++ b/M1/Statistical Learning/TP2_bis_OPTIONAL.ipynb @@ -37,6 +37,7 @@ "outputs": [], "source": [ "import pandas as pd\n", + "\n", "from sklearn import datasets\n", "\n", "iris = datasets.load_iris(as_frame=True)" @@ -402,7 +403,10 @@ "from sklearn.neighbors import KNeighborsClassifier\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(\n", - " X, y, test_size=0.33, random_state=42\n", + " X,\n", + " y,\n", + " test_size=0.33,\n", + " random_state=42,\n", ")\n", "knn_clf = KNeighborsClassifier(n_neighbors=5)\n", "knn_clf.fit(X_train, y_train)\n", @@ -583,7 +587,11 @@ ], "source": [ "X_train_strat, X_test_strat, y_train_strat, y_test_strat = train_test_split(\n", - " X, y, test_size=0.33, random_state=42, stratify=y\n", + " X,\n", + " y,\n", + " test_size=0.33,\n", + " random_state=42,\n", + " stratify=y,\n", ")\n", "\n", "y_test_strat.value_counts()" diff --git a/M1/Statistical Learning/TP3_Logistic_Regression_and _SGD.ipynb b/M1/Statistical Learning/TP3_Logistic_Regression_and _SGD.ipynb index 2730f32..e921135 100644 --- a/M1/Statistical Learning/TP3_Logistic_Regression_and _SGD.ipynb +++ b/M1/Statistical Learning/TP3_Logistic_Regression_and _SGD.ipynb @@ -36,6 +36,7 @@ "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", + "\n", "from sklearn.linear_model import LogisticRegression" ] }, @@ -192,7 +193,10 @@ "plt.figure(figsize=(8, 8))\n", "\n", "plt.scatter(\n", - " X[:, 0], X[:, 1], alpha=0.3, cmap=mcolors.ListedColormap([\"steelblue\", \"tomato\"])\n", + " X[:, 0],\n", + " X[:, 1],\n", + " alpha=0.3,\n", + " cmap=mcolors.ListedColormap([\"steelblue\", \"tomato\"]),\n", ")" ] }, @@ -708,20 +712,22 @@ "outputs": [], "source": [ "def mini_batch_SGD(X, y, learning_rate, batch_size, epochs):\n", - " \"\"\"\n", - " Mini-batch stochastic gradient descent for logistic regression.\n", + " \"\"\"Mini-batch stochastic gradient descent for logistic regression.\n", "\n", - " Parameters:\n", + " Parameters\n", + " ----------\n", " X (numpy.ndarray): Input data of shape (n, d), where n is the number of samples and d is the number of features.\n", " y (numpy.ndarray): Labels of shape (n,), where n is the sample size.\n", " learning_rate (float): Learning rate for gradient descent.\n", " batch_size (int): Size of each mini-batch.\n", " epochs (int): Number of epochs to train.\n", "\n", - " Returns:\n", + " Returns\n", + " -------\n", " w (numpy.ndarray): Final weight vector of shape (d,).\n", " b (float): Final bias term.\n", " costs_SGD (list): Cost function values at each step.\n", + "\n", " \"\"\"\n", " # Initialization\n", " n, d = X.shape\n", @@ -40783,7 +40789,11 @@ ], "source": [ "w_SGD, b_SGD, cost_SGD = mini_batch_SGD(\n", - " X, y, learning_rate=5e-5, batch_size=50, epochs=1000\n", + " X,\n", + " y,\n", + " learning_rate=5e-5,\n", + " batch_size=50,\n", + " epochs=1000,\n", ")" ] }, @@ -40950,7 +40960,11 @@ ], "source": [ "w_SGD, b_SGD, costs_SGD = mini_batch_SGD(\n", - " X, y, learning_rate=5e-4, batch_size=1000, epochs=30\n", + " X,\n", + " y,\n", + " learning_rate=5e-4,\n", + " batch_size=1000,\n", + " epochs=30,\n", ")\n", "\n", "print(\"The parameters computed by stochastic gradient descent are: \", w_SGD, b_SGD)" @@ -41041,7 +41055,10 @@ "\n", "X, y = iris.data, iris.target\n", "X_train, X_test, y_train, y_test = train_test_split(\n", - " X, y, test_size=0.33, random_state=5\n", + " X,\n", + " y,\n", + " test_size=0.33,\n", + " random_state=5,\n", ")\n", "\n", "log_reg = LogisticRegression(fit_intercept=True)\n", @@ -41194,7 +41211,7 @@ "model.add(tf.keras.layers.Input(shape=[28, 28])) # we specify the input shape\n", "model.add(tf.keras.layers.Flatten()) # we flatten the data\n", "model.add(\n", - " tf.keras.layers.Dense(10, activation=\"softmax\")\n", + " tf.keras.layers.Dense(10, activation=\"softmax\"),\n", ") # 10 labels (figures from 0 to 9)\n", "# activation=\"softmax\" as it is a multiclass problem" ] @@ -41237,7 +41254,9 @@ "outputs": [], "source": [ "model.compile(\n", - " loss=\"sparse_categorical_crossentropy\", optimizer=\"sgd\", metrics=[\"accuracy\"]\n", + " loss=\"sparse_categorical_crossentropy\",\n", + " optimizer=\"sgd\",\n", + " metrics=[\"accuracy\"],\n", ")" ] }, diff --git a/M1/Statistical Learning/TP4_Ridge_Lasso_and_CV.ipynb b/M1/Statistical Learning/TP4_Ridge_Lasso_and_CV.ipynb index 59d535f..56bca7c 100644 --- a/M1/Statistical Learning/TP4_Ridge_Lasso_and_CV.ipynb +++ b/M1/Statistical Learning/TP4_Ridge_Lasso_and_CV.ipynb @@ -898,9 +898,9 @@ "ex = pd.DataFrame(\n", " {\n", " \"nom\": [\"Alice\", \"Nicolas\", \"Jean\"],\n", - " \"age\": [19, np.NaN, np.NaN],\n", - " \"exam\": [15, 14, np.NaN],\n", - " }\n", + " \"age\": [19, np.nan, np.nan],\n", + " \"exam\": [15, 14, np.nan],\n", + " },\n", ")\n", "\n", "print(\"data : \\n\", ex)\n", @@ -2299,7 +2299,8 @@ "\n", "linReg = LinearRegression()\n", "linReg.fit(\n", - " Xtrain, Ytrain\n", + " Xtrain,\n", + " Ytrain,\n", ") # no need to scale for OLS if you just want to predict (unless the solver works best with scaled data)\n", "# the predictions should not be different with or without standardization (could differ only owing to numerical problems)\n", "hatY_LinReg = linReg.predict(Xtest)\n", diff --git a/M1/Statistical Learning/TP5_Naive_Bayes.ipynb b/M1/Statistical Learning/TP5_Naive_Bayes.ipynb index b5cc7df..4fe75aa 100644 --- a/M1/Statistical Learning/TP5_Naive_Bayes.ipynb +++ b/M1/Statistical Learning/TP5_Naive_Bayes.ipynb @@ -1246,14 +1246,15 @@ "# 2. Displaying the vectors :\n", "\n", "print(\n", - " \"2. The vectors corresponding to the sms are : \\n\", X.toarray()\n", + " \"2. The vectors corresponding to the sms are : \\n\",\n", + " X.toarray(),\n", ") # X.toarray because\n", "# X is a \"sparse\" matrix.\n", "\n", "# 3. For a new data x_0=\"iphone gratuit\",\n", "# you must also transform x_0 into a numerical vector before predicting.\n", "\n", - "vec_x_0 = vec.transform([\"iphone gratuit\"]).toarray() #\n", + "vec_x_0 = vec.transform([\"iphone gratuit\"]).toarray()\n", "print(\"3. The numerical vector corresponding to (x_0=iphone gratuit) is \\n\", vec_x_0)" ] }, @@ -1410,7 +1411,10 @@ "from sklearn.model_selection import train_test_split\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(\n", - " X, y, test_size=0.30, random_state=50\n", + " X,\n", + " y,\n", + " test_size=0.30,\n", + " random_state=50,\n", ")\n", "\n", "print(\"size of the training set: \", X_train.shape[0])\n", @@ -1986,7 +1990,7 @@ " \"Iphone 15 is now free\",\n", " \"I want coffee\",\n", " \"I want to buy a new iphone\",\n", - " ]\n", + " ],\n", ")\n", "\n", "pred_my_sms = sms_bayes.predict(my_sms)\n", @@ -2055,7 +2059,10 @@ "X_copy = (X.copy() >= 127).astype(int)\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(\n", - " X_copy, y, test_size=0.25, random_state=42\n", + " X_copy,\n", + " y,\n", + " test_size=0.25,\n", + " random_state=42,\n", ")\n", "\n", "ber_bayes = BernoulliNB()\n", diff --git a/M1/Statistical Learning/TP6_keras_intro.ipynb b/M1/Statistical Learning/TP6_keras_intro.ipynb index dc66bd7..8a5f916 100644 --- a/M1/Statistical Learning/TP6_keras_intro.ipynb +++ b/M1/Statistical Learning/TP6_keras_intro.ipynb @@ -92,6 +92,7 @@ "outputs": [], "source": [ "import numpy as np\n", + "\n", "import tensorflow as tf\n", "\n", "tf.keras.utils.set_random_seed(42)\n", @@ -346,7 +347,7 @@ " tf.keras.layers.Dense(300, activation=\"relu\", kernel_initializer=\"he_normal\"),\n", " tf.keras.layers.Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n", " tf.keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")" ] }, @@ -691,7 +692,9 @@ "outputs": [], "source": [ "model.compile(\n", - " loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n", + " loss=\"sparse_categorical_crossentropy\",\n", + " optimizer=\"adam\",\n", + " metrics=[\"accuracy\"],\n", ")" ] }, @@ -1101,11 +1104,13 @@ " tf.keras.layers.Dense(300, activation=\"relu\", kernel_initializer=\"he_normal\"),\n", " tf.keras.layers.Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n", " tf.keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")\n", "\n", "model_10.compile(\n", - " loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n", + " loss=\"sparse_categorical_crossentropy\",\n", + " optimizer=\"adam\",\n", + " metrics=[\"accuracy\"],\n", ")\n", "\n", "model_10.fit(X_train01, y_train, epochs=10, validation_data=(X_val01, y_val))" @@ -1270,7 +1275,8 @@ ], "source": [ "early_stopping_cb = tf.keras.callbacks.EarlyStopping(\n", - " patience=5, restore_best_weights=True\n", + " patience=5,\n", + " restore_best_weights=True,\n", ")\n", "\n", "model = tf.keras.Sequential(\n", @@ -1280,11 +1286,13 @@ " tf.keras.layers.Dense(300, activation=\"relu\", kernel_initializer=\"he_normal\"),\n", " tf.keras.layers.Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n", " tf.keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")\n", "\n", "model.compile(\n", - " loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n", + " loss=\"sparse_categorical_crossentropy\",\n", + " optimizer=\"adam\",\n", + " metrics=[\"accuracy\"],\n", ")\n", "\n", "history2 = model.fit(\n", @@ -1598,10 +1606,12 @@ " tf.keras.layers.Input(shape=[28, 28]),\n", " tf.keras.layers.Flatten(),\n", " tf.keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")\n", "reg_log.compile(\n", - " loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n", + " loss=\"sparse_categorical_crossentropy\",\n", + " optimizer=\"adam\",\n", + " metrics=[\"accuracy\"],\n", ")\n", "reg_log.fit(X_train01, y_train, epochs=90, validation_data=(X_val01, y_val))" ] @@ -1709,10 +1719,12 @@ " tf.keras.layers.Dense(300, activation=\"relu\", kernel_initializer=\"he_normal\"),\n", " tf.keras.layers.Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n", " tf.keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")\n", "model_ter.compile(\n", - " loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n", + " loss=\"sparse_categorical_crossentropy\",\n", + " optimizer=\"adam\",\n", + " metrics=[\"accuracy\"],\n", ")\n", "model_ter.fit(X_train, y_train, epochs=30, validation_data=(X_val, y_val))" ] @@ -1820,10 +1832,12 @@ " tf.keras.layers.Dense(300, activation=\"relu\", kernel_initializer=\"he_normal\"),\n", " tf.keras.layers.Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n", " tf.keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")\n", "model_5.compile(\n", - " loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n", + " loss=\"sparse_categorical_crossentropy\",\n", + " optimizer=\"adam\",\n", + " metrics=[\"accuracy\"],\n", ")\n", "\n", "X_train_far_too_small, X_val_far_too_small = X_train / 25500.0, X_val / 25500.0\n", @@ -1938,16 +1952,22 @@ " tf.keras.layers.Input(shape=[28, 28]),\n", " tf.keras.layers.Flatten(),\n", " tf.keras.layers.Dense(\n", - " 300, activation=\"sigmoid\", kernel_initializer=\"he_normal\"\n", + " 300,\n", + " activation=\"sigmoid\",\n", + " kernel_initializer=\"he_normal\",\n", " ),\n", " tf.keras.layers.Dense(\n", - " 100, activation=\"sigmoid\", kernel_initializer=\"he_normal\"\n", + " 100,\n", + " activation=\"sigmoid\",\n", + " kernel_initializer=\"he_normal\",\n", " ),\n", " tf.keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")\n", "model_sig_norm.compile(\n", - " loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n", + " loss=\"sparse_categorical_crossentropy\",\n", + " optimizer=\"adam\",\n", + " metrics=[\"accuracy\"],\n", ")\n", "model_sig_norm.fit(X_train01, y_train, epochs=30, validation_data=(X_val, y_val))" ] @@ -2043,16 +2063,22 @@ " tf.keras.layers.Input(shape=[28, 28]),\n", " tf.keras.layers.Flatten(),\n", " tf.keras.layers.Dense(\n", - " 300, activation=\"sigmoid\", kernel_initializer=\"he_normal\"\n", + " 300,\n", + " activation=\"sigmoid\",\n", + " kernel_initializer=\"he_normal\",\n", " ),\n", " tf.keras.layers.Dense(\n", - " 100, activation=\"sigmoid\", kernel_initializer=\"he_normal\"\n", + " 100,\n", + " activation=\"sigmoid\",\n", + " kernel_initializer=\"he_normal\",\n", " ),\n", " tf.keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")\n", "model_sig_un_norm.compile(\n", - " loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n", + " loss=\"sparse_categorical_crossentropy\",\n", + " optimizer=\"adam\",\n", + " metrics=[\"accuracy\"],\n", ")\n", "model_sig_un_norm.fit(X_train, y_train, epochs=30, validation_data=(X_val, y_val))" ] @@ -2220,17 +2246,19 @@ " tf.keras.layers.Dense(300, activation=\"relu\"),\n", " tf.keras.layers.Dense(100, activation=\"relu\"),\n", " tf.keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")\n", "model_high_variance.layers[1].set_weights(\n", - " [200 * np.random.randn(28 * 28, 300) / 100, np.zeros(300)]\n", + " [200 * np.random.randn(28 * 28, 300) / 100, np.zeros(300)],\n", ")\n", "model_high_variance.layers[2].set_weights(\n", - " [200 * np.random.randn(300, 100) / 100, np.zeros(100)]\n", + " [200 * np.random.randn(300, 100) / 100, np.zeros(100)],\n", ")\n", "\n", "model_high_variance.compile(\n", - " loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n", + " loss=\"sparse_categorical_crossentropy\",\n", + " optimizer=\"adam\",\n", + " metrics=[\"accuracy\"],\n", ")\n", "\n", "model_high_variance.fit(X_train01, y_train, epochs=60, validation_data=(X_val01, y_val))" diff --git a/M1/Statistical Learning/TP7_Kmeans.ipynb b/M1/Statistical Learning/TP7_Kmeans.ipynb index c5e5fca..6847ea5 100644 --- a/M1/Statistical Learning/TP7_Kmeans.ipynb +++ b/M1/Statistical Learning/TP7_Kmeans.ipynb @@ -543,7 +543,12 @@ "plt.plot(X[:, 0], X[:, 1], \".b\", alpha=0.2)\n", "for center in kmeans1.cluster_centers_:\n", " plt.plot(\n", - " center[0], center[1], \".\", color=\"red\", markersize=10, label=\"Cluster center\"\n", + " center[0],\n", + " center[1],\n", + " \".\",\n", + " color=\"red\",\n", + " markersize=10,\n", + " label=\"Cluster center\",\n", " )\n", "plt.legend()\n", "plt.show()" @@ -623,7 +628,12 @@ "\n", "for center in kmeans1.cluster_centers_:\n", " plt.plot(\n", - " center[0], center[1], \".\", color=\"red\", markersize=10, label=\"Cluster center\"\n", + " center[0],\n", + " center[1],\n", + " \".\",\n", + " color=\"red\",\n", + " markersize=10,\n", + " label=\"Cluster center\",\n", " )\n", "plt.legend()\n", "plt.show()" @@ -1529,9 +1539,10 @@ } ], "source": [ - "import tensorflow as tf\n", "from scipy.stats import mode\n", "\n", + "import tensorflow as tf\n", + "\n", "mnist = tf.keras.datasets.mnist\n", "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n", "\n", @@ -1543,7 +1554,7 @@ "\n", "def map_clusters_to_labels(clusters, true_labels):\n", " return np.array(\n", - " [mode(true_labels[clusters == i], keepdims=True).mode[0] for i in range(10)]\n", + " [mode(true_labels[clusters == i], keepdims=True).mode[0] for i in range(10)],\n", " )\n", "\n", "\n", diff --git a/M1/Statistical Learning/neural_network.ipynb b/M1/Statistical Learning/neural_network.ipynb index cc0b818..1c830f4 100644 --- a/M1/Statistical Learning/neural_network.ipynb +++ b/M1/Statistical Learning/neural_network.ipynb @@ -9,6 +9,7 @@ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", + "\n", "import tensorflow as tf" ] }, @@ -187,10 +188,12 @@ " kernel_regularizer=tf.keras.regularizers.l2(0.01),\n", " ),\n", " tf.keras.layers.Dense(\n", - " 8, activation=\"relu\", kernel_regularizer=tf.keras.regularizers.l2(0.01)\n", + " 8,\n", + " activation=\"relu\",\n", + " kernel_regularizer=tf.keras.regularizers.l2(0.01),\n", " ),\n", " tf.keras.layers.Dense(1, activation=\"sigmoid\"),\n", - " ]\n", + " ],\n", " )\n", " model.compile(optimizer=\"adam\", loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n", " return model" @@ -296,7 +299,10 @@ "histories = []\n", "\n", "early_stopping = EarlyStopping(\n", - " monitor=\"val_loss\", patience=10, restore_best_weights=True, verbose=1\n", + " monitor=\"val_loss\",\n", + " patience=10,\n", + " restore_best_weights=True,\n", + " verbose=1,\n", ")\n", "\n", "for fold, (train_idx, val_idx) in enumerate(skf.split(X, y), 1):\n", @@ -314,7 +320,9 @@ "\n", " # EarlyStopping\n", " callback = tf.keras.callbacks.EarlyStopping(\n", - " monitor=\"val_loss\", patience=10, restore_best_weights=True\n", + " monitor=\"val_loss\",\n", + " patience=10,\n", + " restore_best_weights=True,\n", " )\n", "\n", " # Entraînement\n", @@ -433,13 +441,18 @@ ], "source": [ "import numpy as np\n", + "\n", "import tensorflow as tf\n", "from sklearn.metrics import classification_report, f1_score\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import StandardScaler\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(\n", - " X, y, test_size=0.2, random_state=42, stratify=y\n", + " X,\n", + " y,\n", + " test_size=0.2,\n", + " random_state=42,\n", + " stratify=y,\n", ")\n", "\n", "scaler = StandardScaler()\n", @@ -451,7 +464,9 @@ "model.compile(optimizer=\"adam\", loss=\"binary_crossentropy\")\n", "\n", "callback = tf.keras.callbacks.EarlyStopping(\n", - " monitor=\"val_loss\", patience=10, restore_best_weights=True\n", + " monitor=\"val_loss\",\n", + " patience=10,\n", + " restore_best_weights=True,\n", ")\n", "\n", "history = model.fit(\n", diff --git a/M2/Deep Learning/TP1 - Dense/TP1 - Bonus Starter _ Régularisation.ipynb b/M2/Deep Learning/TP1 - Dense/TP1 - Bonus Starter _ Régularisation.ipynb index be4321d..c18581e 100644 --- a/M2/Deep Learning/TP1 - Dense/TP1 - Bonus Starter _ Régularisation.ipynb +++ b/M2/Deep Learning/TP1 - Dense/TP1 - Bonus Starter _ Régularisation.ipynb @@ -27,25 +27,32 @@ "\n", "sns.set(style=\"whitegrid\")\n", "\n", - "import tensorflow as tf\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import StandardScaler\n", "from tensorflow import keras\n", "\n", "(X_train_full, y_train_full), (X_test, y_test) = keras.datasets.mnist.load_data()\n", "X_train, X_valid, y_train, y_valid = train_test_split(\n", - " X_train_full, y_train_full, train_size=0.8\n", + " X_train_full,\n", + " y_train_full,\n", + " train_size=0.8,\n", ")\n", "\n", "scaler = StandardScaler()\n", "X_train = scaler.fit_transform(X_train.astype(np.float32).reshape(-1, 28 * 28)).reshape(\n", - " -1, 28, 28\n", + " -1,\n", + " 28,\n", + " 28,\n", ")\n", "X_valid = scaler.transform(X_valid.astype(np.float32).reshape(-1, 28 * 28)).reshape(\n", - " -1, 28, 28\n", + " -1,\n", + " 28,\n", + " 28,\n", ")\n", "X_test = scaler.transform(X_test.astype(np.float32).reshape(-1, 28 * 28)).reshape(\n", - " -1, 28, 28\n", + " -1,\n", + " 28,\n", + " 28,\n", ")" ] }, @@ -79,13 +86,17 @@ " keras.layers.Input(shape=[28, 28]),\n", " keras.layers.Flatten(),\n", " keras.layers.Dense(\n", - " 256, activation=\"relu\", kernel_regularizer=keras.regularizers.l2(0.001)\n", + " 256,\n", + " activation=\"relu\",\n", + " kernel_regularizer=keras.regularizers.l2(0.001),\n", " ),\n", " keras.layers.Dense(\n", - " 128, activation=\"relu\", kernel_regularizer=keras.regularizers.l2(0.001)\n", + " 128,\n", + " activation=\"relu\",\n", + " kernel_regularizer=keras.regularizers.l2(0.001),\n", " ),\n", " keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")" ] }, @@ -174,7 +185,7 @@ " kernel_regularizer=keras.regularizers.l2(lambda_l2),\n", " ),\n", " keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", " )\n", " model.compile(\n", " loss=\"sparse_categorical_crossentropy\",\n", @@ -220,7 +231,7 @@ " \"lambda_l2\": lambda_l2,\n", " \"history\": pd.DataFrame(history.history),\n", " \"n_epochs\": n_epochs,\n", - " }\n", + " },\n", " )" ] }, diff --git a/M2/Deep Learning/TP1 - Dense/TP1 - Starter.ipynb b/M2/Deep Learning/TP1 - Dense/TP1 - Starter.ipynb index 0266657..ad60948 100644 --- a/M2/Deep Learning/TP1 - Dense/TP1 - Starter.ipynb +++ b/M2/Deep Learning/TP1 - Dense/TP1 - Starter.ipynb @@ -58,7 +58,10 @@ "from sklearn.model_selection import train_test_split\n", "\n", "X_train, X_valid, y_train, y_valid = train_test_split(\n", - " X_train_full, y_train_full, test_size=0.2, random_state=42\n", + " X_train_full,\n", + " y_train_full,\n", + " test_size=0.2,\n", + " random_state=42,\n", ")\n", "print(X_train.shape, y_train.shape)\n", "print(X_valid.shape, y_valid.shape)" @@ -181,7 +184,7 @@ " keras.layers.Dense(256, activation=\"relu\"),\n", " keras.layers.Dense(128, activation=\"relu\"),\n", " keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")" ] }, @@ -563,7 +566,7 @@ " keras.layers.Dense(256, activation=\"relu\"),\n", " keras.layers.Dense(128, activation=\"relu\"),\n", " keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", " )\n", " model.compile(\n", " loss=\"sparse_categorical_crossentropy\",\n", @@ -673,7 +676,10 @@ " plt.subplot(1, 2, 1)\n", " plt.plot(history_df[\"val_loss\"], linestyle=\"--\", color=colors[_])\n", " plt.plot(\n", - " history_df[\"loss\"], label=f\"LR={learning_rate}\", alpha=0.5, color=colors[_]\n", + " history_df[\"loss\"],\n", + " label=f\"LR={learning_rate}\",\n", + " alpha=0.5,\n", + " color=colors[_],\n", " )\n", " plt.xlabel(\"Epochs\")\n", " plt.ylabel(\"Loss\")\n", diff --git a/M2/Deep Learning/TP2 - Convolution/TP2 - Bonus Starter _ ResNet.ipynb b/M2/Deep Learning/TP2 - Convolution/TP2 - Bonus Starter _ ResNet.ipynb index f037926..3fc7ef4 100644 --- a/M2/Deep Learning/TP2 - Convolution/TP2 - Bonus Starter _ ResNet.ipynb +++ b/M2/Deep Learning/TP2 - Convolution/TP2 - Bonus Starter _ ResNet.ipynb @@ -18,15 +18,12 @@ "outputs": [], "source": [ "import numpy as np\n", - "import pandas as pd\n", "\n", "%matplotlib inline\n", - "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "sns.set(style=\"whitegrid\")\n", "\n", - "import tensorflow as tf\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import StandardScaler\n", "from tensorflow import keras\n", @@ -35,18 +32,29 @@ " keras.datasets.fashion_mnist.load_data()\n", ")\n", "X_train, X_valid, y_train, y_valid = train_test_split(\n", - " X_train_full, y_train_full, train_size=0.8\n", + " X_train_full,\n", + " y_train_full,\n", + " train_size=0.8,\n", ")\n", "\n", "scaler = StandardScaler()\n", "X_train = scaler.fit_transform(X_train.astype(np.float32).reshape(-1, 28 * 28)).reshape(\n", - " -1, 28, 28, 1\n", + " -1,\n", + " 28,\n", + " 28,\n", + " 1,\n", ")\n", "X_valid = scaler.transform(X_valid.astype(np.float32).reshape(-1, 28 * 28)).reshape(\n", - " -1, 28, 28, 1\n", + " -1,\n", + " 28,\n", + " 28,\n", + " 1,\n", ")\n", "X_test = scaler.transform(X_test.astype(np.float32).reshape(-1, 28 * 28)).reshape(\n", - " -1, 28, 28, 1\n", + " -1,\n", + " 28,\n", + " 28,\n", + " 1,\n", ")" ] }, diff --git a/M2/Deep Learning/TP2 - Convolution/TP2 - Starter.ipynb b/M2/Deep Learning/TP2 - Convolution/TP2 - Starter.ipynb index 7966c0e..34d29f1 100644 --- a/M2/Deep Learning/TP2 - Convolution/TP2 - Starter.ipynb +++ b/M2/Deep Learning/TP2 - Convolution/TP2 - Starter.ipynb @@ -60,7 +60,10 @@ "from sklearn.model_selection import train_test_split\n", "\n", "X_train, X_valid, y_train, y_valid = train_test_split(\n", - " X_train_full, y_train_full, test_size=0.2, random_state=42\n", + " X_train_full,\n", + " y_train_full,\n", + " test_size=0.2,\n", + " random_state=42,\n", ")\n", "print(X_train.shape, y_train.shape)\n", "print(X_valid.shape, y_valid.shape)" @@ -178,16 +181,22 @@ " [\n", " keras.layers.Input(shape=(28, 28, 1)),\n", " keras.layers.Conv2D(\n", - " filters=32, kernel_size=3, activation=\"relu\", padding=\"same\"\n", + " filters=32,\n", + " kernel_size=3,\n", + " activation=\"relu\",\n", + " padding=\"same\",\n", " ),\n", " keras.layers.Conv2D(\n", - " filters=32, kernel_size=3, activation=\"relu\", padding=\"same\"\n", + " filters=32,\n", + " kernel_size=3,\n", + " activation=\"relu\",\n", + " padding=\"same\",\n", " ),\n", " keras.layers.MaxPooling2D(pool_size=2, strides=2),\n", " keras.layers.Flatten(),\n", " keras.layers.Dense(units=64, activation=\"relu\"),\n", " keras.layers.Dense(units=10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")" ] }, @@ -374,33 +383,45 @@ " [\n", " keras.layers.Input(shape=(28, 28, 1)),\n", " keras.layers.Conv2D(\n", - " filters=32, kernel_size=3, activation=\"relu\", padding=\"same\"\n", + " filters=32,\n", + " kernel_size=3,\n", + " activation=\"relu\",\n", + " padding=\"same\",\n", " ),\n", " keras.layers.BatchNormalization(),\n", " keras.layers.Conv2D(\n", - " filters=32, kernel_size=3, activation=\"relu\", padding=\"same\"\n", + " filters=32,\n", + " kernel_size=3,\n", + " activation=\"relu\",\n", + " padding=\"same\",\n", " ),\n", " keras.layers.MaxPooling2D(pool_size=2, strides=2),\n", " keras.layers.Flatten(),\n", " keras.layers.Dense(units=64, activation=\"relu\"),\n", " keras.layers.Dense(units=10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", " )\n", " else:\n", " model = keras.models.Sequential(\n", " [\n", " keras.layers.Input(shape=(28, 28, 1)),\n", " keras.layers.Conv2D(\n", - " filters=32, kernel_size=3, activation=\"relu\", padding=\"same\"\n", + " filters=32,\n", + " kernel_size=3,\n", + " activation=\"relu\",\n", + " padding=\"same\",\n", " ),\n", " keras.layers.Conv2D(\n", - " filters=32, kernel_size=3, activation=\"relu\", padding=\"same\"\n", + " filters=32,\n", + " kernel_size=3,\n", + " activation=\"relu\",\n", + " padding=\"same\",\n", " ),\n", " keras.layers.MaxPooling2D(pool_size=2, strides=2),\n", " keras.layers.Flatten(),\n", " keras.layers.Dense(units=64, activation=\"relu\"),\n", " keras.layers.Dense(units=10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", " )\n", "\n", " model.compile(\n", @@ -653,7 +674,9 @@ "outputs": [], "source": [ "def agregate_result(\n", - " results: list, normalized: bool, metric_name: str = \"accuracy\"\n", + " results: list,\n", + " normalized: bool,\n", + " metric_name: str = \"accuracy\",\n", ") -> pd.DataFrame:\n", " train_curves = []\n", " val_curves = []\n", @@ -699,7 +722,9 @@ " ax = axs[idx]\n", " for normalized in [True, False]:\n", " train, val = agregate_result(\n", - " training_curves, normalized=normalized, metric_name=metric\n", + " training_curves,\n", + " normalized=normalized,\n", + " metric_name=metric,\n", " )\n", " train_runs = train.reshape(-1, epochs)\n", " val_runs = val.reshape(-1, epochs)\n", diff --git a/M2/Deep Learning/TP3 - Compléments/TP3 - Starter.ipynb b/M2/Deep Learning/TP3 - Compléments/TP3 - Starter.ipynb index 2948095..0833b40 100644 --- a/M2/Deep Learning/TP3 - Compléments/TP3 - Starter.ipynb +++ b/M2/Deep Learning/TP3 - Compléments/TP3 - Starter.ipynb @@ -20,7 +20,6 @@ "outputs": [], "source": [ "import numpy as np\n", - "import pandas as pd\n", "\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", @@ -300,23 +299,35 @@ " [\n", " keras.layers.InputLayer(shape=(32, 32, 3)),\n", " keras.layers.Conv2D(\n", - " filters=32, kernel_size=3, activation=\"relu\", padding=\"same\"\n", + " filters=32,\n", + " kernel_size=3,\n", + " activation=\"relu\",\n", + " padding=\"same\",\n", " ),\n", " keras.layers.Dropout(0.2),\n", " keras.layers.Conv2D(\n", - " filters=32, kernel_size=3, activation=\"relu\", padding=\"same\"\n", + " filters=32,\n", + " kernel_size=3,\n", + " activation=\"relu\",\n", + " padding=\"same\",\n", " ),\n", " keras.layers.MaxPooling2D(pool_size=2),\n", " keras.layers.Conv2D(\n", - " filters=16, kernel_size=3, activation=\"relu\", padding=\"same\"\n", + " filters=16,\n", + " kernel_size=3,\n", + " activation=\"relu\",\n", + " padding=\"same\",\n", " ),\n", " keras.layers.Dropout(0.2),\n", " keras.layers.Conv2D(\n", - " filters=16, kernel_size=3, activation=\"relu\", padding=\"same\"\n", + " filters=16,\n", + " kernel_size=3,\n", + " activation=\"relu\",\n", + " padding=\"same\",\n", " ),\n", " keras.layers.Flatten(),\n", " keras.layers.Dense(10, activation=\"softmax\"),\n", - " ]\n", + " ],\n", " )\n", "\n", " return model\n", @@ -348,7 +359,9 @@ "outputs": [], "source": [ "def compile_train(\n", - " optimizer_function: str, learning_rate: float, **kwargs\n", + " optimizer_function: str,\n", + " learning_rate: float,\n", + " **kwargs,\n", ") -> keras.callbacks.History:\n", " model = get_model()\n", " optimizer = optimizer_function(learning_rate=learning_rate)\n", @@ -401,7 +414,10 @@ "epochs = 5\n", "batch_size = 64\n", "history_adam = compile_train(\n", - " keras.optimizers.Adam, learning_rate=0.001, epochs=epochs, batch_size=batch_size\n", + " keras.optimizers.Adam,\n", + " learning_rate=0.001,\n", + " epochs=epochs,\n", + " batch_size=batch_size,\n", ")" ] }, @@ -557,7 +573,10 @@ "histories = []\n", "for optimizer in optimizers:\n", " history = compile_train(\n", - " optimizer, learning_rate=learning_rate, epochs=epochs, batch_size=batch_size\n", + " optimizer,\n", + " learning_rate=learning_rate,\n", + " epochs=epochs,\n", + " batch_size=batch_size,\n", " )\n", " name = optimizer.__name__\n", " label = f\"{name} (lr={learning_rate:.06})\"\n", diff --git a/M2/Deep Learning/TP4 - Récurrents/TP4 - Bonus.ipynb b/M2/Deep Learning/TP4 - Récurrents/TP4 - Bonus.ipynb index 56b3f1c..96b0e73 100644 --- a/M2/Deep Learning/TP4 - Récurrents/TP4 - Bonus.ipynb +++ b/M2/Deep Learning/TP4 - Récurrents/TP4 - Bonus.ipynb @@ -17,10 +17,11 @@ "metadata": {}, "outputs": [], "source": [ - "import keras\n", "import numpy as np\n", "import seaborn as sns\n", "\n", + "import keras\n", + "\n", "sns.set(style=\"whitegrid\")\n", "\n", "\n", @@ -149,7 +150,7 @@ " keras.layers.Embedding(\n", " input_dim=vocabulary_size,\n", " output_dim=dimension,\n", - " )\n", + " ),\n", " )\n", " model.add(keras.layers.SimpleRNN(128, return_sequences=False))\n", " model.add(keras.layers.Dense(vocabulary_size, activation=\"softmax\"))\n", diff --git a/M2/Deep Learning/TP4 - Récurrents/TP4 - Starter.ipynb b/M2/Deep Learning/TP4 - Récurrents/TP4 - Starter.ipynb index 2e7bc28..8f1d333 100644 --- a/M2/Deep Learning/TP4 - Récurrents/TP4 - Starter.ipynb +++ b/M2/Deep Learning/TP4 - Récurrents/TP4 - Starter.ipynb @@ -120,9 +120,7 @@ }, "outputs": [], "source": [ - "character_to_index = {\n", - " character: index for index, character in enumerate(characters)\n", - "}\n", + "character_to_index = {character: index for index, character in enumerate(characters)}\n", "index_to_character = dict(enumerate(characters))" ] }, @@ -317,7 +315,7 @@ " keras.layers.SimpleRNN(128, return_sequences=False),\n", " # Ajouter une couche Dense\n", " keras.layers.Dense(n_characters, activation=\"softmax\"),\n", - " ]\n", + " ],\n", ")\n", "\n", "model.summary()" @@ -429,11 +427,14 @@ "print(len(epochs), len(historic[\"loss\"]))\n", "\n", "for index, (metric_name, axis) in enumerate(\n", - " zip([\"loss\", \"accuracy\"], [axis_1, axis_2], strict=False)\n", + " zip([\"loss\", \"accuracy\"], [axis_1, axis_2], strict=False),\n", "):\n", " color = sns.color_palette()[index]\n", " axis.plot(\n", - " epochs[: len(historic[metric_name])], historic[metric_name], lw=2, color=color\n", + " epochs[: len(historic[metric_name])],\n", + " historic[metric_name],\n", + " lw=2,\n", + " color=color,\n", " )\n", " axis.plot(\n", " epochs[: len(historic[\"val_\" + metric_name])],\n", @@ -604,7 +605,8 @@ "outputs": [], "source": [ "random_index = np.random.multinomial(\n", - " 1, y_test[np.random.randint(0, len(X_test) - 1)].ravel()\n", + " 1,\n", + " y_test[np.random.randint(0, len(X_test) - 1)].ravel(),\n", ").argmax()" ] }, diff --git a/M2/Machine Learning/TP_1/2025_TP_1_M2_ISF.ipynb b/M2/Machine Learning/TP_1/2025_TP_1_M2_ISF.ipynb index a4a66f0..62cd9cf 100644 --- a/M2/Machine Learning/TP_1/2025_TP_1_M2_ISF.ipynb +++ b/M2/Machine Learning/TP_1/2025_TP_1_M2_ISF.ipynb @@ -144,7 +144,8 @@ ], "source": [ "salutation = \"Bonjour, monsieur {}. Comment allez vous en ce {}?\".format(\n", - " \"XX\", \"Mardi 19 septembre\"\n", + " \"XX\",\n", + " \"Mardi 19 septembre\",\n", ")\n", "print(salutation)" ] @@ -259,7 +260,7 @@ ], "source": [ "a = 2\n", - "if 5 > a:\n", + "if a < 5:\n", " print(\"Cinq!\")\n", "else:\n", " print(\"a!\")\n", @@ -2612,11 +2613,10 @@ " binaires.append(col)\n", " else:\n", " quantitatives.append(col)\n", + " elif len(data_set[col].dropna().unique()) == 2:\n", + " binaires.append(col)\n", " else:\n", - " if len(data_set[col].dropna().unique()) == 2:\n", - " binaires.append(col)\n", - " else:\n", - " categorielles.append(col)\n", + " categorielles.append(col)\n", "\n", "print(\"Variables quantitatives :\", quantitatives)\n", "print(\"\\nVariables catégorielles :\", categorielles)\n", @@ -3527,7 +3527,7 @@ "source": [ "fig = px.histogram(data_set.sort_values(\"ANNEE_CTR\"), x=\"ANNEE_CTR\")\n", "fig.update_xaxes(\n", - " type=\"category\"\n", + " type=\"category\",\n", ") # Cette ligne permet de forcer la variable comme variable catégorielle et non numérique\n", "\n", "fig.show()" @@ -18662,7 +18662,7 @@ " data_set,\n", " x=\"CONTRAT_ANCIENNETE\",\n", " category_orders={\n", - " \"CONTRAT_ANCIENNETE\": [\"(-1,0]\", \"(0,1]\", \"(1,2]\", \"(2,5]\", \"(5,10]\"]\n", + " \"CONTRAT_ANCIENNETE\": [\"(-1,0]\", \"(0,1]\", \"(1,2]\", \"(2,5]\", \"(5,10]\"],\n", " },\n", ")\n", "fig.show()" @@ -48902,7 +48902,7 @@ " data_set,\n", " x=\"GROUPE_KM\",\n", " category_orders={\n", - " \"GROUPE_KM\": [\"[0;20000[\", \"[20000;40000[\", \"[40000;60000[\", \"[60000;99999[\"]\n", + " \"GROUPE_KM\": [\"[0;20000[\", \"[20000;40000[\", \"[40000;60000[\", \"[60000;99999[\"],\n", " },\n", ")\n", "fig.show()" @@ -64021,7 +64021,7 @@ "# Ecrivez votre code ici\n", "fig = px.histogram(data_set.sort_values(\"ZONE_RISQUE\"), x=\"ZONE_RISQUE\")\n", "fig.update_xaxes(\n", - " type=\"category\"\n", + " type=\"category\",\n", ") # Cette ligne permet de forcer la variable comme variable catégorielle et non numérique\n", "\n", "fig.show()" @@ -64877,10 +64877,11 @@ "source": [ "# Ecrivez votre code ici\n", "fig = px.histogram(\n", - " data_set.sort_values(\"AGE_ASSURE_PRINCIPAL\"), x=\"AGE_ASSURE_PRINCIPAL\"\n", + " data_set.sort_values(\"AGE_ASSURE_PRINCIPAL\"),\n", + " x=\"AGE_ASSURE_PRINCIPAL\",\n", ")\n", "fig.update_xaxes(\n", - " type=\"category\"\n", + " type=\"category\",\n", ") # Cette ligne permet de forcer la variable comme variable catégorielle et non numérique\n", "\n", "fig.show()" @@ -79999,7 +80000,7 @@ "# Ecrivez votre code ici\n", "fig = px.histogram(data_set.sort_values(\"GENRE\"), x=\"GENRE\")\n", "fig.update_xaxes(\n", - " type=\"category\"\n", + " type=\"category\",\n", ") # Cette ligne permet de forcer la variable comme variable catégorielle et non numérique\n", "\n", "fig.show()" @@ -80064,7 +80065,8 @@ " [\n", " data_h,\n", " pd.DataFrame(\n", - " [[13, \"M\", 0]], columns=[\"AGE_ASSURE_PRINCIPAL\", \"GENRE\", \"counts\"]\n", + " [[13, \"M\", 0]],\n", + " columns=[\"AGE_ASSURE_PRINCIPAL\", \"GENRE\", \"counts\"],\n", " ),\n", " ],\n", " ignore_index=True,\n", @@ -82329,17 +82331,17 @@ "# ANNEE_CONSTRUCTION,VALEUR_DU_BIEN,DEUXIEME_CONDUCTEUR)\n", "\n", "data_retraitee[\"GROUPE_KM\"] = data_retraitee[\"GROUPE_KM\"].fillna(\n", - " data_retraitee[\"GROUPE_KM\"].mode()[0]\n", + " data_retraitee[\"GROUPE_KM\"].mode()[0],\n", ")\n", "data_retraitee[\"GENRE\"] = data_retraitee[\"GENRE\"].fillna(\"M\")\n", "data_retraitee[\"ANNEE_CONSTRUCTION\"] = data_retraitee[\"ANNEE_CONSTRUCTION\"].fillna(\n", - " data_retraitee[\"ANNEE_CONSTRUCTION\"].median()\n", + " data_retraitee[\"ANNEE_CONSTRUCTION\"].median(),\n", ")\n", "data_retraitee[\"VALEUR_DU_BIEN\"] = data_retraitee[\"VALEUR_DU_BIEN\"].fillna(\n", - " data_retraitee[\"VALEUR_DU_BIEN\"].mode()[0]\n", + " data_retraitee[\"VALEUR_DU_BIEN\"].mode()[0],\n", ")\n", "data_retraitee[\"DEUXIEME_CONDUCTEUR\"] = data_retraitee[\"DEUXIEME_CONDUCTEUR\"].fillna(\n", - " False\n", + " False,\n", ")" ] }, @@ -83750,7 +83752,10 @@ "source": [ "# Représentation graphique\n", "fig = px.line(\n", - " plot_data, x=\"AGE_ASSURE_PRINCIPAL\", y=\"FREQ\", title=\"Sinistralité selon l'âge\"\n", + " plot_data,\n", + " x=\"AGE_ASSURE_PRINCIPAL\",\n", + " y=\"FREQ\",\n", + " title=\"Sinistralité selon l'âge\",\n", ")\n", "fig.show()" ] @@ -85539,7 +85544,10 @@ "\n", "# Représentation graphique\n", "fig = px.scatter(\n", - " plot_data, x=\"ENERGIE\", y=\"FREQ\", title=\"Sinistralité selon le carburant\"\n", + " plot_data,\n", + " x=\"ENERGIE\",\n", + " y=\"FREQ\",\n", + " title=\"Sinistralité selon le carburant\",\n", ")\n", "fig.show()" ] @@ -86416,7 +86424,10 @@ "\n", "# Représentation graphique\n", "fig = px.scatter(\n", - " plot_data, x=\"VALEUR_DU_BIEN\", y=\"CM\", title=\"Coût moyen selon le prix\"\n", + " plot_data,\n", + " x=\"VALEUR_DU_BIEN\",\n", + " y=\"CM\",\n", + " title=\"Coût moyen selon le prix\",\n", ")\n", "fig.show()" ] @@ -89044,7 +89055,10 @@ "\n", "# Représentation graphique\n", "fig = px.scatter(\n", - " plot_data, x=\"GENRE\", y=\"CM\", title=\"Coût moyen selon l'âge de l'assuré\"\n", + " plot_data,\n", + " x=\"GENRE\",\n", + " y=\"CM\",\n", + " title=\"Coût moyen selon l'âge de l'assuré\",\n", ")\n", "fig.show()" ] diff --git a/M2/Machine Learning/TP_2/2025_TP_2_M2_ISF.ipynb b/M2/Machine Learning/TP_2/2025_TP_2_M2_ISF.ipynb index 2c1f48f..e202adb 100644 --- a/M2/Machine Learning/TP_2/2025_TP_2_M2_ISF.ipynb +++ b/M2/Machine Learning/TP_2/2025_TP_2_M2_ISF.ipynb @@ -56,16 +56,15 @@ "import seaborn as sns\n", "\n", "sns.set()\n", - "import matplotlib.pyplot as plt # noqa: E402\n", + "import matplotlib.pyplot as plt\n", "import plotly.express as px\n", - "import plotly.graph_objects as gp\n", - "from scipy.cluster.hierarchy import dendrogram, linkage # noqa: E402\n", + "from scipy.cluster.hierarchy import dendrogram, linkage\n", "\n", "# Statistiques\n", - "from scipy.stats import chi2_contingency # noqa: E402, F401\n", + "from scipy.stats import chi2_contingency # noqa: F401\n", "\n", "# Machine Learning\n", - "from sklearn.cluster import AgglomerativeClustering, KMeans # noqa: E402" + "from sklearn.cluster import AgglomerativeClustering, KMeans" ] }, { @@ -899,7 +898,9 @@ "source": [ "# Calcul de la partition de l'espace\n", "hierarchical_cluster = AgglomerativeClustering(\n", - " n_clusters=3, metric=\"euclidean\", linkage=\"single\"\n", + " n_clusters=3,\n", + " metric=\"euclidean\",\n", + " linkage=\"single\",\n", ")\n", "\n", "labels = hierarchical_cluster.fit_predict(data)\n", @@ -972,7 +973,9 @@ "source": [ "# Calcul de la partition de l'espace\n", "hierarchical_cluster = AgglomerativeClustering(\n", - " n_clusters=3, metric=\"euclidean\", linkage=\"complete\"\n", + " n_clusters=3,\n", + " metric=\"euclidean\",\n", + " linkage=\"complete\",\n", ")\n", "\n", "labels = hierarchical_cluster.fit_predict(data)\n", @@ -1482,7 +1485,7 @@ "\n", "# Group by ZONE_RISQUE and aggregate the necessary columns\n", "data = data_retraitee.groupby([\"ZONE_RISQUE\"], as_index=False).agg(\n", - " {\"NB\": \"sum\", \"CHARGE\": \"sum\", \"EXPO\": \"sum\"}\n", + " {\"NB\": \"sum\", \"CHARGE\": \"sum\", \"EXPO\": \"sum\"},\n", ")\n", "\n", "# Calculate derived metrics\n", @@ -1547,7 +1550,11 @@ "source": [ "# Initialisation de l'algorithme\n", "kmeans_FREQ = KMeans(\n", - " init=\"random\", n_clusters=5, n_init=1, random_state=42, max_iter=300\n", + " init=\"random\",\n", + " n_clusters=5,\n", + " n_init=1,\n", + " random_state=42,\n", + " max_iter=300,\n", ")\n", "\n", "# Transformation des données : plusieurs échantillons de 1 dimension\n", @@ -3559,7 +3566,11 @@ "source": [ "# Initialisation de l'algorithme\n", "kmeans_FREQ_CM = KMeans(\n", - " init=\"random\", n_clusters=5, n_init=1, random_state=42, max_iter=300\n", + " init=\"random\",\n", + " n_clusters=5,\n", + " n_init=1,\n", + " random_state=42,\n", + " max_iter=300,\n", ")\n", "\n", "# Transformation des données : plusieurs échantillons de 1 dimension\n", @@ -4621,7 +4632,9 @@ "source": [ "# Calcul de la partition de l'espace\n", "hierarchical_cluster = AgglomerativeClustering(\n", - " n_clusters=5, metric=\"euclidean\", linkage=\"single\"\n", + " n_clusters=5,\n", + " metric=\"euclidean\",\n", + " linkage=\"single\",\n", ")\n", "\n", "labels = hierarchical_cluster.fit_predict(data_x)\n", @@ -5650,7 +5663,9 @@ "source": [ "# Calcul de la partition de l'espace\n", "hierarchical_cluster = AgglomerativeClustering(\n", - " n_clusters=5, metric=\"euclidean\", linkage=\"single\"\n", + " n_clusters=5,\n", + " metric=\"euclidean\",\n", + " linkage=\"single\",\n", ")\n", "\n", "labels = hierarchical_cluster.fit_predict(data_x)\n", diff --git a/M2/Machine Learning/TP_3/2025_TP_3_M2_ISF.ipynb b/M2/Machine Learning/TP_3/2025_TP_3_M2_ISF.ipynb index a45a084..4908acf 100644 --- a/M2/Machine Learning/TP_3/2025_TP_3_M2_ISF.ipynb +++ b/M2/Machine Learning/TP_3/2025_TP_3_M2_ISF.ipynb @@ -60,12 +60,13 @@ "\n", "sns.set()\n", "import plotly.express as px\n", - "import sklearn.metrics as metrics\n", - "import sklearn.preprocessing as preproc\n", "\n", "# Statistiques\n", "from scipy.stats import chi2_contingency\n", "\n", + "import sklearn.preprocessing as preproc\n", + "from sklearn import metrics\n", + "\n", "# Machine Learning\n", "from sklearn.ensemble import RandomForestRegressor\n", "from sklearn.model_selection import KFold, cross_val_score, train_test_split\n", @@ -89,7 +90,7 @@ "source": [ "def cramers_V(var1, var2):\n", " crosstab = np.array(\n", - " pd.crosstab(var1, var2, rownames=None, colnames=None)\n", + " pd.crosstab(var1, var2, rownames=None, colnames=None),\n", " ) # Cross table building\n", " stat = chi2_contingency(crosstab)[\n", " 0\n", @@ -2027,17 +2028,15 @@ "for colu in data_set.columns:\n", " if True in data_set[colu].isna().unique():\n", " variables_na.append(data_set[colu])\n", - " else:\n", - " if str(data_set[colu].dtypes) in [\"int32\", \"int64\", \"float64\"]:\n", - " if len(data_set[colu].unique()) == 2:\n", - " variables_categorielles.append(data_set[colu])\n", - " else:\n", - " variables_numeriques.append(data_set[colu])\n", + " elif str(data_set[colu].dtypes) in [\"int32\", \"int64\", \"float64\"]:\n", + " if len(data_set[colu].unique()) == 2:\n", + " variables_categorielles.append(data_set[colu])\n", " else:\n", - " if len(data_set[colu].unique()) == 2:\n", - " variables_categorielles.append(data_set[colu])\n", - " else:\n", - " variables_categorielles.append(data_set[colu])" + " variables_numeriques.append(data_set[colu])\n", + " elif len(data_set[colu].unique()) == 2:\n", + " variables_categorielles.append(data_set[colu])\n", + " else:\n", + " variables_categorielles.append(data_set[colu])" ] }, { @@ -2437,7 +2436,8 @@ " col = []\n", " for var2 in vars_categorielles:\n", " cramers = cramers_V(\n", - " vars_categorielles[var1], vars_categorielles[var2]\n", + " vars_categorielles[var1],\n", + " vars_categorielles[var2],\n", " ) # V de Cramer\n", " col.append(round(cramers, 2)) # arrondi du résultat\n", " rows.append(col)\n", @@ -2468,7 +2468,7 @@ " + \" et \"\n", " + v_cramer_resultats.columns[j]\n", " + \" sont trop dépendantes, V-CRAMER = \"\n", - " + str(v_cramer_resultats.iloc[i, j])\n", + " + str(v_cramer_resultats.iloc[i, j]),\n", " )" ] }, @@ -2662,7 +2662,7 @@ " + \" et \"\n", " + correlations_num.columns[j]\n", " + \" sont trop dépendantes, corr = \"\n", - " + str(correlations_num.iloc[i, j])\n", + " + str(correlations_num.iloc[i, j]),\n", " )" ] }, @@ -3312,7 +3312,7 @@ "# One hot encoding des variables catégorielles\n", "preproc_ohe = preproc.OneHotEncoder(handle_unknown=\"ignore\")\n", "preproc_ohe = preproc.OneHotEncoder(drop=\"first\", sparse_output=False).fit(\n", - " vars_categorielles\n", + " vars_categorielles,\n", ")\n", "\n", "variables_categorielles_ohe = preproc_ohe.transform(vars_categorielles)\n", @@ -3496,7 +3496,8 @@ "\n", "vars_numeriques_scaled = preproc_scale.transform(vars_numeriques)\n", "vars_numeriques_scaled = pd.DataFrame(\n", - " vars_numeriques_scaled, columns=vars_numeriques.columns\n", + " vars_numeriques_scaled,\n", + " columns=vars_numeriques.columns,\n", ")\n", "vars_numeriques_scaled.head()" ] @@ -3525,7 +3526,9 @@ "outputs": [], "source": [ "X_global = vars_numeriques_scaled.merge(\n", - " variables_categorielles_ohe, left_index=True, right_index=True\n", + " variables_categorielles_ohe,\n", + " left_index=True,\n", + " right_index=True,\n", ")" ] }, @@ -3542,7 +3545,10 @@ "\n", "# Sampling en 80% train et 20% test\n", "X_train, X_test, y_train, y_test = train_test_split(\n", - " X, Y, test_size=0.2, random_state=42\n", + " X,\n", + " Y,\n", + " test_size=0.2,\n", + " random_state=42,\n", ")" ] }, @@ -3707,7 +3713,9 @@ "outputs": [], "source": [ "X_global = vars_numeriques_scaled.merge(\n", - " variables_categorielles_ohe, left_index=True, right_index=True\n", + " variables_categorielles_ohe,\n", + " left_index=True,\n", + " right_index=True,\n", ")\n", "\n", "# Réorganisation des données\n", @@ -3888,7 +3896,9 @@ "outputs": [], "source": [ "X_global = vars_numeriques_scaled.merge(\n", - " variables_categorielles_ohe, left_index=True, right_index=True\n", + " variables_categorielles_ohe,\n", + " left_index=True,\n", + " right_index=True,\n", ")\n", "# Réorganisation des données\n", "X = X_global.to_numpy()\n", @@ -4127,6 +4137,7 @@ "outputs": [], "source": [ "import numpy as np\n", + "\n", "from sklearn.ensemble import RandomForestRegressor\n", "from sklearn.model_selection import GridSearchCV, KFold" ] @@ -4140,7 +4151,10 @@ "source": [ "# Sampling en 80% train et 20% test\n", "X_train, X_test, y_train, y_test = train_test_split(\n", - " X, Y, test_size=0.2, random_state=42\n", + " X,\n", + " Y,\n", + " test_size=0.2,\n", + " random_state=42,\n", ")" ] }, @@ -4186,7 +4200,9 @@ " estimator=rf,\n", " param_grid=param_grid,\n", " cv=KFold(\n", - " n_splits=num_folds, shuffle=True, random_state=42\n", + " n_splits=num_folds,\n", + " shuffle=True,\n", + " random_state=42,\n", " ), # Validation croisée avec 5 folds\n", " scoring=\"neg_mean_squared_error\", # Métrique d'évaluation (moins c'est mieux)\n", " n_jobs=-1, # Utiliser tous les cœurs du processeur\n", @@ -4247,7 +4263,11 @@ "# Cross validation\n", "# RMSE de chaque fold\n", "rmse_scores = cross_val_score(\n", - " best_rf, X_train, y_train, cv=num_folds, scoring=\"neg_root_mean_squared_error\"\n", + " best_rf,\n", + " X_train,\n", + " y_train,\n", + " cv=num_folds,\n", + " scoring=\"neg_root_mean_squared_error\",\n", ")\n", "\n", "# Afficher les scores pour chaque fold\n", @@ -4256,7 +4276,11 @@ "\n", "# MSE de chaque fold\n", "mse_scores = cross_val_score(\n", - " best_rf, X_train, y_train, cv=num_folds, scoring=\"neg_mean_squared_error\"\n", + " best_rf,\n", + " X_train,\n", + " y_train,\n", + " cv=num_folds,\n", + " scoring=\"neg_mean_squared_error\",\n", ")\n", "\n", "# Afficher les scores pour chaque fold\n", @@ -4266,7 +4290,11 @@ "\n", "# MAE de chaque fold\n", "mae_scores = cross_val_score(\n", - " best_rf, X_train, y_train, cv=num_folds, scoring=\"neg_mean_absolute_error\"\n", + " best_rf,\n", + " X_train,\n", + " y_train,\n", + " cv=num_folds,\n", + " scoring=\"neg_mean_absolute_error\",\n", ")\n", "\n", "# Afficher les scores pour chaque fold\n", diff --git a/M2/Machine Learning/TP_4/2025_M2_ISF_TP_4.ipynb b/M2/Machine Learning/TP_4/2025_M2_ISF_TP_4.ipynb index 70c1932..3cee985 100644 --- a/M2/Machine Learning/TP_4/2025_M2_ISF_TP_4.ipynb +++ b/M2/Machine Learning/TP_4/2025_M2_ISF_TP_4.ipynb @@ -58,14 +58,15 @@ "import seaborn as sns\n", "\n", "sns.set()\n", - "import plotly.express as px\n", - "\n", - "# Machine Learning\n", - "import sklearn.preprocessing as preproc\n", "from imblearn.over_sampling import RandomOverSampler\n", "\n", + "import plotly.express as px\n", + "\n", "# Statistiques\n", "from scipy.stats import chi2_contingency\n", + "\n", + "# Machine Learning\n", + "import sklearn.preprocessing as preproc\n", "from sklearn import metrics\n", "from sklearn.ensemble import GradientBoostingClassifier\n", "from sklearn.model_selection import (\n", @@ -93,7 +94,7 @@ "source": [ "def cramers_V(var1, var2):\n", " crosstab = np.array(\n", - " pd.crosstab(var1, var2, rownames=None, colnames=None)\n", + " pd.crosstab(var1, var2, rownames=None, colnames=None),\n", " ) # Cross table building\n", " stat = chi2_contingency(crosstab)[\n", " 0\n", @@ -16171,7 +16172,9 @@ "source": [ "# Observation de la distribution\n", "fig = px.histogram(\n", - " data_model, x=\"SINISTRE\", title=\"Distribution de la variable 'sinistré'\"\n", + " data_model,\n", + " x=\"SINISTRE\",\n", + " title=\"Distribution de la variable 'sinistré'\",\n", ")\n", "fig.show()" ] @@ -16221,17 +16224,15 @@ "for col in data_set.columns:\n", " if True in data_model[col].isna().unique():\n", " variables_na.append(data_model[col])\n", - " else:\n", - " if str(data_model[col].dtypes) in [\"int32\", \"int64\", \"float64\"]:\n", - " if len(data_model[col].unique()) == 2:\n", - " variables_categorielles.append(data_model[col])\n", - " else:\n", - " variables_numeriques.append(data_model[col])\n", + " elif str(data_model[col].dtypes) in [\"int32\", \"int64\", \"float64\"]:\n", + " if len(data_model[col].unique()) == 2:\n", + " variables_categorielles.append(data_model[col])\n", " else:\n", - " if len(data_model[col].unique()) == 2:\n", - " variables_categorielles.append(data_model[col])\n", - " else:\n", - " variables_categorielles.append(data_model[col])" + " variables_numeriques.append(data_model[col])\n", + " elif len(data_model[col].unique()) == 2:\n", + " variables_categorielles.append(data_model[col])\n", + " else:\n", + " variables_categorielles.append(data_model[col])" ] }, { @@ -16631,7 +16632,8 @@ " col = []\n", " for var2 in vars_categorielles:\n", " cramers = cramers_V(\n", - " vars_categorielles[var1], vars_categorielles[var2]\n", + " vars_categorielles[var1],\n", + " vars_categorielles[var2],\n", " ) # V de Cramer\n", " col.append(round(cramers, 2)) # arrondi du résultat\n", " rows.append(col)\n", @@ -16658,7 +16660,7 @@ " for j in range(i + 1, v_cramer_resultats.shape[0]):\n", " if v_cramer_resultats.iloc[i, j] > 0.7:\n", " print(\n", - " f\"{v_cramer_resultats.index.to_numpy()[i]} et {v_cramer_resultats.colmns[j]} sont trop dépendantes, V-CRAMER = {v_cramer_resultats.iloc[i, j]}\"\n", + " f\"{v_cramer_resultats.index.to_numpy()[i]} et {v_cramer_resultats.colmns[j]} sont trop dépendantes, V-CRAMER = {v_cramer_resultats.iloc[i, j]}\",\n", " )" ] }, @@ -16856,7 +16858,7 @@ " for j in range(i + 1, nb_variables):\n", " if abs(correlations_num.iloc[i, j]) > 0.7:\n", " print(\n", - " f\"{correlations_num.index.to_numpy()[i]} et {correlations_num.columns[j]} sont trop dépendantes, corr = {correlations_num.iloc[i, j]}\"\n", + " f\"{correlations_num.index.to_numpy()[i]} et {correlations_num.columns[j]} sont trop dépendantes, corr = {correlations_num.iloc[i, j]}\",\n", " )" ] }, @@ -17520,7 +17522,7 @@ "# One hot encoding des variables catégorielles\n", "preproc_ohe = preproc.OneHotEncoder(handle_unknown=\"ignore\")\n", "preproc_ohe = preproc.OneHotEncoder(drop=\"first\", sparse_output=False).fit(\n", - " vars_categorielles\n", + " vars_categorielles,\n", ")\n", "\n", "variables_categorielles_ohe = preproc_ohe.transform(vars_categorielles)\n", @@ -17704,7 +17706,8 @@ "\n", "vars_numeriques_scaled = preproc_scale.transform(vars_numeriques)\n", "vars_numeriques_scaled = pd.DataFrame(\n", - " vars_numeriques_scaled, columns=vars_numeriques.columns\n", + " vars_numeriques_scaled,\n", + " columns=vars_numeriques.columns,\n", ")\n", "vars_numeriques_scaled.head()" ] @@ -17756,7 +17759,9 @@ "outputs": [], "source": [ "X_global = vars_numeriques_scaled.merge(\n", - " variables_categorielles_ohe, left_index=True, right_index=True\n", + " variables_categorielles_ohe,\n", + " left_index=True,\n", + " right_index=True,\n", ")\n", "# Réorganisation des données\n", "X = X_global.to_numpy()\n", @@ -17772,7 +17777,11 @@ "source": [ "# Sampling en 80% train et 20% test\n", "X_train, X_test, y_train, y_test = train_test_split(\n", - " X, Y, test_size=0.2, random_state=42, stratify=Y\n", + " X,\n", + " Y,\n", + " test_size=0.2,\n", + " random_state=42,\n", + " stratify=Y,\n", ")" ] }, @@ -17824,7 +17833,9 @@ " estimator=gbc,\n", " param_grid=param_grid,\n", " cv=StratifiedKFold(\n", - " n_splits=num_folds, shuffle=True, random_state=42\n", + " n_splits=num_folds,\n", + " shuffle=True,\n", + " random_state=42,\n", " ), # Validation croisée avec 5 folds\n", " scoring=\"recall\", # Métrique d'évaluation (moins c'est mieux)\n", " n_jobs=-1, # Utiliser tous les cœurs du processeur\n", @@ -17884,7 +17895,11 @@ "source": [ "# Recall de chaque fold\n", "recall_scores = cross_val_score(\n", - " best_gbc, X_train, y_train, cv=num_folds, scoring=\"recall\"\n", + " best_gbc,\n", + " X_train,\n", + " y_train,\n", + " cv=num_folds,\n", + " scoring=\"recall\",\n", ")\n", "\n", "# Afficher les scores pour chaque fold\n", @@ -17893,7 +17908,11 @@ "\n", "# Accuracy de chaque fold\n", "accuracy_scores = cross_val_score(\n", - " best_gbc, X_train, y_train, cv=num_folds, scoring=\"accuracy\"\n", + " best_gbc,\n", + " X_train,\n", + " y_train,\n", + " cv=num_folds,\n", + " scoring=\"accuracy\",\n", ")\n", "\n", "# Afficher les scores pour chaque fold\n", @@ -17903,7 +17922,11 @@ "\n", "# Precision de chaque fold\n", "precision_scores = cross_val_score(\n", - " best_gbc, X_train, y_train, cv=num_folds, scoring=\"precision\"\n", + " best_gbc,\n", + " X_train,\n", + " y_train,\n", + " cv=num_folds,\n", + " scoring=\"precision\",\n", ")\n", "\n", "# Afficher les scores pour chaque fold\n", @@ -52512,7 +52535,9 @@ "# Observation de la distribution sur Y_train\n", "df = pd.DataFrame(y_train_resampled, columns=[\"SINISTRE\"])\n", "fig = px.histogram(\n", - " df, x=\"SINISTRE\", title=\"Distribution de la variable Y_train_resampled\"\n", + " df,\n", + " x=\"SINISTRE\",\n", + " title=\"Distribution de la variable Y_train_resampled\",\n", ")\n", "fig.show()" ] @@ -52565,7 +52590,9 @@ " estimator=gb,\n", " param_grid=param_grid,\n", " cv=StratifiedKFold(\n", - " n_splits=num_folds, shuffle=True, random_state=42\n", + " n_splits=num_folds,\n", + " shuffle=True,\n", + " random_state=42,\n", " ), # Validation croisée stratifiée avec 5 plis\n", " scoring=\"recall\", # Métrique d'évaluation\n", " n_jobs=-1, # Utiliser tous les cœurs du processeur\n", @@ -52618,7 +52645,11 @@ "# Zoom sur la CV\n", "# Recall de chaque fold\n", "recall_scores = cross_val_score(\n", - " best_gbc, X_train_resampled, y_train_resampled, cv=num_folds, scoring=\"recall\"\n", + " best_gbc,\n", + " X_train_resampled,\n", + " y_train_resampled,\n", + " cv=num_folds,\n", + " scoring=\"recall\",\n", ")\n", "\n", "# Afficher les scores pour chaque fold\n", @@ -52627,7 +52658,11 @@ "\n", "# Accuracy de chaque fold\n", "accuracy_scores = cross_val_score(\n", - " best_gbc, X_train_resampled, y_train_resampled, cv=num_folds, scoring=\"accuracy\"\n", + " best_gbc,\n", + " X_train_resampled,\n", + " y_train_resampled,\n", + " cv=num_folds,\n", + " scoring=\"accuracy\",\n", ")\n", "\n", "# Afficher les scores pour chaque fold\n", @@ -52637,7 +52672,11 @@ "\n", "# Precision de chaque fold\n", "precision_scores = cross_val_score(\n", - " best_gbc, X_train_resampled, y_train_resampled, cv=num_folds, scoring=\"precision\"\n", + " best_gbc,\n", + " X_train_resampled,\n", + " y_train_resampled,\n", + " cv=num_folds,\n", + " scoring=\"precision\",\n", ")\n", "\n", "# Afficher les scores pour chaque fold\n", @@ -53146,7 +53185,8 @@ "# Matrice de confusion\n", "confusion_matrix = metrics.confusion_matrix(y_test, y_pred)\n", "cm_display = metrics.ConfusionMatrixDisplay(\n", - " confusion_matrix=confusion_matrix, display_labels=[False, True]\n", + " confusion_matrix=confusion_matrix,\n", + " display_labels=[False, True],\n", ")\n", "\n", "cm_display.plot()" diff --git a/M2/Machine Learning/TP_5/2025_M2_ISF_TP_5.ipynb b/M2/Machine Learning/TP_5/2025_M2_ISF_TP_5.ipynb index 422b443..241c0d0 100644 --- a/M2/Machine Learning/TP_5/2025_M2_ISF_TP_5.ipynb +++ b/M2/Machine Learning/TP_5/2025_M2_ISF_TP_5.ipynb @@ -115,6 +115,7 @@ "import numpy as np\n", "import pandas as pd\n", "import seaborn as sns\n", + "\n", "from catboost import CatBoostClassifier, Pool\n", "from sklearn.metrics import (\n", " classification_report,\n", @@ -163,10 +164,10 @@ "\n", "print(\"=== Chargement du dataset Adult Income ===\\n\")\n", "print(\n", - " \"Dataset classique de Kaggle/UCI qui illustre parfaitement les forces de CatBoost\"\n", + " \"Dataset classique de Kaggle/UCI qui illustre parfaitement les forces de CatBoost\",\n", ")\n", "print(\n", - " \"Objectif : Prédire si le revenu annuel > 50K$ basé sur des caractéristiques socio-démographiques\\n\"\n", + " \"Objectif : Prédire si le revenu annuel > 50K$ basé sur des caractéristiques socio-démographiques\\n\",\n", ")\n", "\n", "# Chargement depuis UCI\n", @@ -192,7 +193,11 @@ "\n", "try:\n", " df = pd.read_csv(\n", - " url, names=column_names, sep=r\",\\s*\", engine=\"python\", na_values=\"?\"\n", + " url,\n", + " names=column_names,\n", + " sep=r\",\\s*\",\n", + " engine=\"python\",\n", + " na_values=\"?\",\n", " )\n", " print(\"Dataset chargé depuis UCI repository\")\n", "except: # noqa: E722\n", @@ -301,7 +306,7 @@ " n_samples,\n", " p=[0.90, 0.02, 0.01, 0.01, 0.01, 0.01, 0.04],\n", " ),\n", - " }\n", + " },\n", " )\n", "\n", " # Création de la cible avec logique réaliste\n", @@ -647,19 +652,25 @@ "\n", "# Taux de revenu >50K par catégorie\n", "df.groupby(\"education\")[\"income\"].mean().sort_values(ascending=False).plot(\n", - " kind=\"barh\", ax=axes[0, 0], color=\"skyblue\"\n", + " kind=\"barh\",\n", + " ax=axes[0, 0],\n", + " color=\"skyblue\",\n", ")\n", "axes[0, 0].set_title(\"Taux de revenu >50K par niveau d'éducation\")\n", "axes[0, 0].set_xlabel(\"Taux\")\n", "\n", "df.groupby(\"occupation\")[\"income\"].mean().sort_values(ascending=False).plot(\n", - " kind=\"barh\", ax=axes[0, 1], color=\"lightcoral\"\n", + " kind=\"barh\",\n", + " ax=axes[0, 1],\n", + " color=\"lightcoral\",\n", ")\n", "axes[0, 1].set_title(\"Taux de revenu >50K par occupation\")\n", "axes[0, 1].set_xlabel(\"Taux\")\n", "\n", "df.groupby(\"marital_status\")[\"income\"].mean().sort_values(ascending=False).plot(\n", - " kind=\"barh\", ax=axes[0, 2], color=\"lightgreen\"\n", + " kind=\"barh\",\n", + " ax=axes[0, 2],\n", + " color=\"lightgreen\",\n", ")\n", "axes[0, 2].set_title(\"Taux de revenu >50K par statut marital\")\n", "axes[0, 2].set_xlabel(\"Taux\")\n", @@ -758,7 +769,11 @@ "\n", "# Split train/test stratifié\n", "X_train, X_test, y_train, y_test = train_test_split(\n", - " X, y, test_size=0.2, random_state=42, stratify=y\n", + " X,\n", + " y,\n", + " test_size=0.2,\n", + " random_state=42,\n", + " stratify=y,\n", ")" ] }, @@ -1020,7 +1035,7 @@ "feature_names = X_train.columns\n", "\n", "importance_df = pd.DataFrame(\n", - " {\"feature\": feature_names, \"importance\": feature_importance}\n", + " {\"feature\": feature_names, \"importance\": feature_importance},\n", ").sort_values(\"importance\", ascending=False)\n", "\n", "print(importance_df)\n", @@ -1283,7 +1298,10 @@ "y_reg = df[\"montant_defaut\"]\n", "\n", "X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(\n", - " X_reg, y_reg, test_size=0.2, random_state=42\n", + " X_reg,\n", + " y_reg,\n", + " test_size=0.2,\n", + " random_state=42,\n", ")\n", "\n", "# Pools\n", @@ -1292,7 +1310,11 @@ "\n", "# Modèle\n", "model_reg = CatBoostRegressor(\n", - " iterations=500, learning_rate=0.1, depth=6, random_seed=42, verbose=100\n", + " iterations=500,\n", + " learning_rate=0.1,\n", + " depth=6,\n", + " random_seed=42,\n", + " verbose=100,\n", ")\n", "\n", "model_reg.fit(train_pool_reg, eval_set=test_pool_reg)\n", @@ -1363,7 +1385,7 @@ "feature_names = X_reg.columns\n", "\n", "importance_df = pd.DataFrame(\n", - " {\"feature\": feature_names, \"importance\": feature_importance}\n", + " {\"feature\": feature_names, \"importance\": feature_importance},\n", ").sort_values(\"importance\", ascending=False)\n", "\n", "print(importance_df)\n", @@ -1580,7 +1602,7 @@ "# Importance SHAP moyenne\n", "shap_importance = np.abs(shap_values[:, :-1]).mean(axis=0)\n", "shap_df = pd.DataFrame(\n", - " {\"feature\": X_train.columns, \"shap_importance\": shap_importance}\n", + " {\"feature\": X_train.columns, \"shap_importance\": shap_importance},\n", ").sort_values(\"shap_importance\", ascending=False)\n", "\n", "print(\"\\nImportance SHAP moyenne :\")\n", diff --git a/M2/Reinforcement Learning/Lab 1 - Multi‑Armed Bandits with epsilon‑Greedy and Bernoulli Rewards.ipynb b/M2/Reinforcement Learning/Lab 1 - Multi‑Armed Bandits with epsilon‑Greedy and Bernoulli Rewards.ipynb index d0ee6f0..9966b7f 100644 --- a/M2/Reinforcement Learning/Lab 1 - Multi‑Armed Bandits with epsilon‑Greedy and Bernoulli Rewards.ipynb +++ b/M2/Reinforcement Learning/Lab 1 - Multi‑Armed Bandits with epsilon‑Greedy and Bernoulli Rewards.ipynb @@ -299,14 +299,15 @@ "\n", " # With probability 1−ε: exploit (choose an arm with the highest estimated value).\n", " max_val = np.max(\n", - " Q\n", + " Q,\n", " ) # Compute the maximum value of the array Q and store it in the variable max_val\n", " candidates = np.isclose(\n", - " Q, max_val\n", + " Q,\n", + " max_val,\n", " ) # (see Hint) Find all positions in Q where the value equals max_val.\n", "\n", " return np.random.choice(\n", - " candidates\n", + " candidates,\n", " ) # pick one of those best arms uniformly at random." ] }, @@ -614,13 +615,16 @@ "\n", " # record what happens at each time step\n", " rewards = np.zeros(\n", - " T, dtype=float\n", + " T,\n", + " dtype=float,\n", " ) # rewards[t] = observed reward at step t (0 or 1),\n", " chose_opt = np.zeros(\n", - " T, dtype=float\n", + " T,\n", + " dtype=float,\n", " ) # chose_opt[t] = 1 if the chosen arm equals opt_arm, else 0,\n", " regret = np.zeros(\n", - " T, dtype=float\n", + " T,\n", + " dtype=float,\n", " ) # regret[t]=p^*-R_t, which means how much we “missed” compared to the best arm\n", "\n", " # -------------------------------------------------\n", @@ -714,7 +718,11 @@ "outputs": [], "source": [ "def run_many(\n", - " runs: int, T: int, epsilon: float, k: int, update_style: str = \"incremental\"\n", + " runs: int,\n", + " T: int,\n", + " epsilon: float,\n", + " k: int,\n", + " update_style: str = \"incremental\",\n", "):\n", " \"\"\"Run multiple independent experiments of T time steps using ε-greedy on a k-armed Bernoulli bandit.\n", "\n", @@ -844,7 +852,9 @@ "plt.figure(figsize=(10, 6))\n", "for eps in eps_list:\n", " plt.plot(\n", - " results[eps][\"avg_reward\"], label=f\"ε={eps}\", color=colors[eps_list.index(eps)]\n", + " results[eps][\"avg_reward\"],\n", + " label=f\"ε={eps}\",\n", + " color=colors[eps_list.index(eps)],\n", " )\n", "plt.xlabel(\"Step\")\n", "plt.ylabel(\"Average reward\")\n", @@ -856,7 +866,9 @@ "plt.figure(figsize=(10, 6))\n", "for eps in eps_list:\n", " plt.plot(\n", - " results[eps][\"avg_opt\"], label=f\"ε={eps}\", color=colors[eps_list.index(eps)]\n", + " results[eps][\"avg_opt\"],\n", + " label=f\"ε={eps}\",\n", + " color=colors[eps_list.index(eps)],\n", " )\n", "plt.xlabel(\"Step\")\n", "plt.ylabel(\"P(select optimal arm)\")\n", @@ -881,7 +893,9 @@ "plt.figure(figsize=(10, 6))\n", "for eps in eps_list:\n", " plt.plot(\n", - " results[eps][\"avg_cumreg\"], label=f\"ε={eps}\", color=colors[eps_list.index(eps)]\n", + " results[eps][\"avg_cumreg\"],\n", + " label=f\"ε={eps}\",\n", + " color=colors[eps_list.index(eps)],\n", " )\n", "plt.xlabel(\"Step\")\n", "plt.ylabel(\"Average cumulative regret\")\n", @@ -929,7 +943,7 @@ "# Calculate final performance metrics for each epsilon\n", "print(\"### Performance Summary for Different ε Values\\n\")\n", "print(\n", - " f\"{'ε':<6} {'Final Avg Reward':<18} {'Final Opt %':<15} {'Final Cum Reward':<18} {'Final Cum Regret':<18}\"\n", + " f\"{'ε':<6} {'Final Avg Reward':<18} {'Final Opt %':<15} {'Final Cum Reward':<18} {'Final Cum Regret':<18}\",\n", ")\n", "print(\"-\" * 80)\n", "\n", @@ -940,7 +954,7 @@ " final_cum_regret = results[eps][\"avg_cumreg\"][-1]\n", "\n", " print(\n", - " f\"{eps:<6.2f} {final_avg_reward:<18.4f} {final_opt_prob:<15.2f} {final_cum_reward:<18.2f} {final_cum_regret:<18.2f}\"\n", + " f\"{eps:<6.2f} {final_avg_reward:<18.4f} {final_opt_prob:<15.2f} {final_cum_reward:<18.2f} {final_cum_regret:<18.2f}\",\n", " )\n", "\n", "# Find the best epsilon based on multiple criteria\n", @@ -1029,10 +1043,10 @@ "t_incr = time_runner(\"incremental\")\n", "\n", "print(\n", - " f\"Naive sample-mean total time over {RUNS_num} runs × {T_time} steps: {t_naive:.3f} s\"\n", + " f\"Naive sample-mean total time over {RUNS_num} runs × {T_time} steps: {t_naive:.3f} s\",\n", ")\n", "print(\n", - " f\"Incremental sample-mean total time over {RUNS_num} runs × {T_time} steps: {t_incr:.3f} s\"\n", + " f\"Incremental sample-mean total time over {RUNS_num} runs × {T_time} steps: {t_incr:.3f} s\",\n", ")\n", "print(f\"Speedup (naive / incremental): {t_naive / t_incr:.2f}×\")" ] diff --git a/M2/Reinforcement Learning/Lab 2 - Maze Game as a Markov Decision Process Part 1.ipynb b/M2/Reinforcement Learning/Lab 2 - Maze Game as a Markov Decision Process Part 1.ipynb index 38b504a..10354b9 100644 --- a/M2/Reinforcement Learning/Lab 2 - Maze Game as a Markov Decision Process Part 1.ipynb +++ b/M2/Reinforcement Learning/Lab 2 - Maze Game as a Markov Decision Process Part 1.ipynb @@ -40,11 +40,12 @@ "import numpy as np\n", "\n", "np.set_printoptions(\n", - " precision=3, suppress=True\n", + " precision=3,\n", + " suppress=True,\n", ") # (not mandatory) This line is for limiting floats to 3 decimal places, avoiding scientific notation (like 1.23e-04) for small numbers.\n", "\n", "# For reproducibility\n", - "rng = np.random.default_rng(seed=42) # This line creates a random number generator.\n" + "rng = np.random.default_rng(seed=42) # This line creates a random number generator." ] }, { @@ -110,7 +111,7 @@ " \"#..#..G\",\n", " \"#..X..#\",\n", " \"#######\",\n", - "]\n" + "]" ] }, { @@ -142,7 +143,7 @@ "n_rows = len(maze_str)\n", "print(n_rows)\n", "n_cols = len(maze_str[0])\n", - "print(n_cols)\n" + "print(n_cols)" ] }, { @@ -169,7 +170,7 @@ "source": [ "print(\"Maze:\")\n", "for row in maze_str:\n", - " print(row)\n" + " print(row)" ] }, { @@ -207,7 +208,7 @@ " \"S\",\n", " \"G\",\n", " \"X\",\n", - "} # The vector Free represents cells that the agent is allowed to move into.\n" + "} # The vector Free represents cells that the agent is allowed to move into." ] }, { @@ -277,7 +278,7 @@ "print(\"Number of states (non-wall cells):\", n_states)\n", "print(\"Start state:\", start_state, \"at\", state_to_pos[start_state])\n", "print(\"Goal states:\", goal_states, \"at\", state_to_pos[goal_states[0]])\n", - "print(\"Trap states:\", trap_states, \"at\", state_to_pos[trap_states[0]])\n" + "print(\"Trap states:\", trap_states, \"at\", state_to_pos[trap_states[0]])" ] }, { @@ -304,7 +305,7 @@ ], "source": [ "my_dict = {\"key1\": \"value1\", \"key2\": \"value2\"}\n", - "print(my_dict[\"key2\"])\n" + "print(my_dict[\"key2\"])" ] }, { @@ -383,7 +384,7 @@ "def plot_maze_with_states():\n", " \"\"\"Plot the maze with state indices.\"\"\"\n", " grid = np.ones(\n", - " (n_rows, n_cols)\n", + " (n_rows, n_cols),\n", " ) # Start with a matrix of ones. Here 1 means “free cell”\n", " for i in range(n_rows):\n", " for j in range(n_cols):\n", @@ -571,7 +572,7 @@ " # If the next cell is a wall, the robot stays in place.\n", " return i, j\n", "\n", - " return candidate_i, candidate_j # Otherwise, return the new position\n" + " return candidate_i, candidate_j # Otherwise, return the new position" ] }, { @@ -601,7 +602,7 @@ "outputs": [], "source": [ "gamma = 0.95\n", - "p_error = 0.1 # probability of the error to a random other direction\n" + "p_error = 0.1 # probability of the error to a random other direction" ] }, { @@ -662,7 +663,7 @@ "# Set rewards for each state\n", "step_penalty = -0.01\n", "goal_reward = 1.0\n", - "trap_reward = -1.0\n" + "trap_reward = -1.0" ] }, { @@ -710,7 +711,7 @@ " elif s in trap_states:\n", " R[s] = trap_reward\n", " else:\n", - " R[s] = step_penalty\n" + " R[s] = step_penalty" ] }, { @@ -735,7 +736,7 @@ "\n", "def is_terminal(s: int) -> bool:\n", " \"\"\"Check if a state is terminal (goal or trap).\"\"\"\n", - " return s in terminal_states\n" + " return s in terminal_states" ] }, { @@ -797,9 +798,9 @@ " error_i, error_j = move_deterministic(i, j, a2)\n", " s_error = pos_to_state[(error_i, error_j)] # get its state index s_error\n", " P[a, s, s_error] += p_error / len(\n", - " other_actions\n", + " other_actions,\n", " ) # add p_error / 3 to P[a, s, s_error]\n", - "# So for each (s,a), probabilities over all s_next sum to 1.\n" + "# So for each (s,a), probabilities over all s_next sum to 1." ] }, { @@ -843,7 +844,7 @@ " # If everything is correct, they should be very close to 1.\n", "\n", " probs = P[a].sum(axis=1)\n", - " print(f\"Action {action_names[a]}:\", probs)\n" + " print(f\"Action {action_names[a]}:\", probs)" ] }, { @@ -995,7 +996,7 @@ "\n", " for _it in range(max_iter): # Main iterative loop\n", " V_new = np.zeros_like(\n", - " V\n", + " V,\n", " ) # Create a new value vector and we will compute an updated value for each state.\n", "\n", " # Now we update each state using the Bellman expectation equation\n", @@ -1004,7 +1005,7 @@ " V_new[s] = R[s] + gamma * np.sum(P[a, s, :] * V)\n", "\n", " delta = np.max(\n", - " np.abs(V_new - V)\n", + " np.abs(V_new - V),\n", " ) # This measures how much the value function changed in this iteration:\n", " # If delta is small, the values start to converge; otherwise, we need to keep iterating.\n", " V = V_new # Update V, i.e. Set the new values for the next iteration.\n", @@ -1012,7 +1013,7 @@ " if delta < theta: # Check convergence: When changes are tiny, we stop.\n", " break\n", "\n", - " return V # Return the final value function, this is our estimate for V^{pi}(s), s in the state set.\n" + " return V # Return the final value function, this is our estimate for V^{pi}(s), s in the state set." ] }, { @@ -1082,7 +1083,7 @@ "source": [ "V_random = policy_evaluation(policy=random_policy, P=P, R=R, gamma=gamma)\n", "print(\"Value function under random policy:\")\n", - "print(V_random)\n" + "print(V_random)" ] }, { @@ -1127,7 +1128,8 @@ "def plot_values(V: np.ndarray, title=\"Value function\") -> None:\n", " \"\"\"Plot the value function V on the maze as a heatmap.\"\"\"\n", " grid_values = np.full(\n", - " (n_rows, n_cols), np.nan\n", + " (n_rows, n_cols),\n", + " np.nan,\n", " ) # Initializes a grid the same size as the maze. Every cell starts as NaN.\n", " for (\n", " s,\n", @@ -1152,7 +1154,13 @@ "\n", " for s, (i, j) in state_to_pos.items():\n", " ax.text(\n", - " j, i, f\"{V[s]:.2f}\", ha=\"center\", va=\"center\", color=\"white\", fontsize=9\n", + " j,\n", + " i,\n", + " f\"{V[s]:.2f}\",\n", + " ha=\"center\",\n", + " va=\"center\",\n", + " color=\"white\",\n", + " fontsize=9,\n", " )\n", "\n", " # Remove axis ticks and set title\n", @@ -1162,7 +1170,7 @@ " plt.show()\n", "\n", "\n", - "plot_values(V_random, title=\"Value function: random policy\")\n" + "plot_values(V_random, title=\"Value function: random policy\")" ] }, { @@ -1247,7 +1255,7 @@ " ax.set_yticklabels([])\n", " ax.grid(True)\n", " ax.set_title(title)\n", - " plt.show()\n" + " plt.show()" ] }, { @@ -1276,7 +1284,7 @@ } ], "source": [ - "plot_policy(policy=random_policy, title=\"Policy\")\n" + "plot_policy(policy=random_policy, title=\"Policy\")" ] }, { diff --git a/M2/Reinforcement Learning/Lab 2 - Second maze.ipynb b/M2/Reinforcement Learning/Lab 2 - Second maze.ipynb index d38a544..709100b 100644 --- a/M2/Reinforcement Learning/Lab 2 - Second maze.ipynb +++ b/M2/Reinforcement Learning/Lab 2 - Second maze.ipynb @@ -23,11 +23,12 @@ "import numpy as np\n", "\n", "np.set_printoptions(\n", - " precision=3, suppress=True\n", + " precision=3,\n", + " suppress=True,\n", ") # (not mandatory) This line is for limiting floats to 3 decimal places, avoiding scientific notation (like 1.23e-04) for small numbers.\n", "\n", "# For reproducibility\n", - "rng = np.random.default_rng(seed=42) # This line creates a random number generator.\n" + "rng = np.random.default_rng(seed=42) # This line creates a random number generator." ] }, { @@ -102,7 +103,7 @@ " \"S\",\n", " \"G\",\n", " \"X\",\n", - "} # The vector Free represents cells that the agent is allowed to move into.\n" + "} # The vector Free represents cells that the agent is allowed to move into." ] }, { @@ -164,7 +165,7 @@ "print(\"Number of states (non-wall cells):\", n_states)\n", "print(\"Start state:\", start_state, \"at\", state_to_pos[start_state])\n", "print(\"Goal states:\", goal_states, \"at\", state_to_pos[goal_states[0]])\n", - "print(\"Trap states:\", trap_states, \"at\", state_to_pos[trap_states[0]])\n" + "print(\"Trap states:\", trap_states, \"at\", state_to_pos[trap_states[0]])" ] }, { @@ -188,7 +189,7 @@ "def plot_maze_with_states():\n", " \"\"\"Plot the maze with state indices.\"\"\"\n", " grid = np.ones(\n", - " (n_rows, n_cols)\n", + " (n_rows, n_cols),\n", " ) # Start with a matrix of ones. Here 1 means “free cell”\n", " for i in range(n_rows):\n", " for j in range(n_cols):\n", @@ -316,7 +317,7 @@ " # If the next cell is a wall, the robot stays in place.\n", " return i, j\n", "\n", - " return candidate_i, candidate_j # Otherwise, return the new position\n" + " return candidate_i, candidate_j # Otherwise, return the new position" ] }, { @@ -335,7 +336,7 @@ "outputs": [], "source": [ "gamma = 0.95\n", - "p_error = 0.1 # probability of the error to a random other direction\n" + "p_error = 0.1 # probability of the error to a random other direction" ] }, { @@ -360,7 +361,7 @@ "# Set rewards for each state\n", "step_penalty = -0.01\n", "goal_reward = 1.0\n", - "trap_reward = -1.0\n" + "trap_reward = -1.0" ] }, { @@ -376,7 +377,7 @@ " elif s in trap_states:\n", " R[s] = trap_reward\n", " else:\n", - " R[s] = step_penalty\n" + " R[s] = step_penalty" ] }, { @@ -391,7 +392,7 @@ "\n", "def is_terminal(s: int) -> bool:\n", " \"\"\"Check if a state is terminal (goal or trap).\"\"\"\n", - " return s in terminal_states\n" + " return s in terminal_states" ] }, { @@ -437,9 +438,9 @@ " error_i, error_j = move_deterministic(i, j, a2)\n", " s_error = pos_to_state[(error_i, error_j)] # get its state index s_error\n", " P[a, s, s_error] += p_error / len(\n", - " other_actions\n", + " other_actions,\n", " ) # add p_error / 3 to P[a, s, s_error]\n", - "# So for each (s,a), probabilities over all s_next sum to 1.\n" + "# So for each (s,a), probabilities over all s_next sum to 1." ] }, { @@ -476,7 +477,7 @@ " # If everything is correct, they should be very close to 1.\n", "\n", " probs = P[a].sum(axis=1)\n", - " print(f\"Action {action_names[a]}:\", probs)\n" + " print(f\"Action {action_names[a]}:\", probs)" ] }, { @@ -520,7 +521,7 @@ "\n", " for _it in range(max_iter): # Main iterative loop\n", " V_new = np.zeros_like(\n", - " V\n", + " V,\n", " ) # Create a new value vector and we will compute an updated value for each state.\n", "\n", " # Now we update each state using the Bellman expectation equation\n", @@ -529,7 +530,7 @@ " V_new[s] = R[s] + gamma * np.sum(P[a, s, :] * V)\n", "\n", " delta = np.max(\n", - " np.abs(V_new - V)\n", + " np.abs(V_new - V),\n", " ) # This measures how much the value function changed in this iteration:\n", " # If delta is small, the values start to converge; otherwise, we need to keep iterating.\n", " V = V_new # Update V, i.e. Set the new values for the next iteration.\n", @@ -537,7 +538,7 @@ " if delta < theta: # Check convergence: When changes are tiny, we stop.\n", " break\n", "\n", - " return V # Return the final value function, this is our estimate for V^{pi}(s), s in the state set.\n" + " return V # Return the final value function, this is our estimate for V^{pi}(s), s in the state set." ] }, { @@ -550,7 +551,8 @@ "def plot_values(V: np.ndarray, title=\"Value function\") -> None:\n", " \"\"\"Plot the value function V on the maze as a heatmap.\"\"\"\n", " grid_values = np.full(\n", - " (n_rows, n_cols), np.nan\n", + " (n_rows, n_cols),\n", + " np.nan,\n", " ) # Initializes a grid the same size as the maze. Every cell starts as NaN.\n", " for (\n", " s,\n", @@ -575,14 +577,20 @@ "\n", " for s, (i, j) in state_to_pos.items():\n", " ax.text(\n", - " j, i, f\"{V[s]:.2f}\", ha=\"center\", va=\"center\", color=\"white\", fontsize=9\n", + " j,\n", + " i,\n", + " f\"{V[s]:.2f}\",\n", + " ha=\"center\",\n", + " va=\"center\",\n", + " color=\"white\",\n", + " fontsize=9,\n", " )\n", "\n", " # Remove axis ticks and set title\n", " ax.set_xticks([])\n", " ax.set_yticks([])\n", " ax.set_title(title)\n", - " plt.show()\n" + " plt.show()" ] }, { @@ -659,7 +667,7 @@ " ax.set_yticklabels([])\n", " ax.grid(True)\n", " ax.set_title(title)\n", - " plt.show()\n" + " plt.show()" ] }, { @@ -716,7 +724,7 @@ "source": [ "V_random = policy_evaluation(policy=random_policy, P=P, R=R, gamma=gamma)\n", "print(\"Value function under random policy:\")\n", - "print(V_random)\n" + "print(V_random)" ] }, { @@ -748,7 +756,7 @@ ], "source": [ "plot_values(V_random, title=\"Value function: random policy\")\n", - "plot_policy(policy=random_policy, title=\"Random Policy\")\n" + "plot_policy(policy=random_policy, title=\"Random Policy\")" ] }, { @@ -847,7 +855,7 @@ "V_my_policy = policy_evaluation(policy=my_policy, P=P, R=R, gamma=gamma)\n", "\n", "plot_values(V=V_my_policy, title=\"Value function: my policy\")\n", - "plot_policy(policy=my_policy, title=\"My policy\")\n" + "plot_policy(policy=my_policy, title=\"My policy\")" ] }, { diff --git a/M2/Unsupervised Learning/spectral_clustering.ipynb b/M2/Unsupervised Learning/spectral_clustering.ipynb index f62ac89..38cf447 100644 --- a/M2/Unsupervised Learning/spectral_clustering.ipynb +++ b/M2/Unsupervised Learning/spectral_clustering.ipynb @@ -9,6 +9,7 @@ "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", + "\n", "from sklearn.cluster import KMeans" ] }, @@ -77,11 +78,11 @@ " return eigenvalues[idx_L], eigenvectors[:, idx_L]\n", "\n", "\n", - "def compute_W_matrix(sigma: int, X: np.ndarray) -> np.ndarray:\n", + "def compute_W_matrix(sigma: float, X: np.ndarray) -> np.ndarray:\n", " \"\"\"Fill the similarity matrix W.\n", "\n", " Args:\n", - " sigma (int): Parameter for the Gaussian kernel.\n", + " sigma (float): Parameter for the Gaussian kernel.\n", " X (np.ndarray): Input data.\n", "\n", " Returns:\n", @@ -94,7 +95,7 @@ " 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" + " return W" ] }, { @@ -104,11 +105,11 @@ "metadata": {}, "outputs": [], "source": [ - "def create_X(sigma: int, n: int, m: int) -> np.ndarray:\n", + "def create_X(sigma: float, n: int, m: int) -> np.ndarray:\n", " \"\"\"Create a dataset with 4 Gaussian clusters.\n", "\n", " Args:\n", - " sigma (int): Standard deviation of the clusters.\n", + " sigma (float): Standard deviation of the clusters.\n", " n (int): Total number of data points.\n", " m (int): Number of clusters.\n", "\n", @@ -125,11 +126,11 @@ " return np.concatenate([norm_1, norm_2, norm_3, norm_4])\n", "\n", "\n", - "def plot_eigenvalues(sigma: int, n: int, m: int, k: int) -> None:\n", + "def plot_eigenvalues(sigma: float, n: int, m: int, k: int) -> None:\n", " \"\"\"Plot the eigenvalues of the Laplacian for different sigma values.\n", "\n", " Args:\n", - " sigma (int): Standard deviation of the clusters.\n", + " sigma (float): Standard deviation of the clusters.\n", " n (int): Total number of data points.\n", " m (int): Number of clusters.\n", " k (int): Number of eigenvalues to compute.\n", @@ -153,7 +154,7 @@ " plt.xlabel(\"Index\")\n", " plt.ylabel(\"Eigenvalue\")\n", "\n", - " plt.show()\n" + " plt.show()" ] }, { @@ -236,7 +237,7 @@ "n = 200\n", "m = 4\n", "for sigma in [0.1, 1 / 4, 0.5, 1]:\n", - " plot_eigenvalues(sigma, n=n, m=m, k=k)\n" + " plot_eigenvalues(sigma, n=n, m=m, k=k)" ] }, { @@ -246,14 +247,14 @@ "metadata": {}, "outputs": [], "source": [ - "def spectral_clustering(sigma, n: int, m: int, k: int) -> np.ndarray:\n", + "def spectral_clustering(sigma: float, n: int, m: int, k: int) -> np.ndarray:\n", " \"\"\"Perform spectral clustering on the data.\n", "\n", " Args:\n", - " sigma: The sigma value for the similarity matrix.\n", - " n: Number of data points.\n", - " m: Number of clusters.\n", - " k: Number of eigenvectors to use.\n", + " sigma (float): The sigma value for the similarity matrix.\n", + " n (int): Number of data points.\n", + " m (int): Number of clusters.\n", + " k (int): Number of eigenvectors to use.\n", "\n", " Returns:\n", " X: The data points.\n", @@ -269,7 +270,7 @@ "\n", " kmeans = KMeans(n_clusters=k)\n", "\n", - " return X, kmeans.fit_predict(U_normalized)\n" + " return X, kmeans.fit_predict(U_normalized)" ] }, { @@ -312,7 +313,7 @@ "\n", "plt.scatter(X, np.zeros_like(X), c=clusters, cmap=\"magma\")\n", "plt.title(\"Spectral Clustering Results\")\n", - "plt.xlabel(\"Data Points\")\n" + "plt.xlabel(\"Data Points\")" ] }, {