mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-01-14 15:54:13 +01:00
Refactor code in numerical methods notebooks
- Updated import order in Point_Fixe.ipynb for consistency. - Changed lambda functions to regular function definitions for clarity in Point_Fixe.ipynb. - Added numpy import in TP1_EDO_EulerExp.ipynb, TP2_Lokta_Volterra.ipynb, and TP3_Convergence.ipynb for better readability. - Modified for loops in TP1_EDO_EulerExp.ipynb and TP2_Lokta_Volterra.ipynb to include strict=False for compatibility with future Python versions.
This commit is contained in:
@@ -9,8 +9,8 @@
|
||||
"%matplotlib inline\n",
|
||||
"%config InlineBackend.figure_format = 'retina'\n",
|
||||
"\n",
|
||||
"import numpy as np # pour les numpy array\n",
|
||||
"import matplotlib.pyplot as plt # librairie graphique\n",
|
||||
"import numpy as np # pour les numpy array\n",
|
||||
"from scipy.integrate import odeint # seulement odeint"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -19,8 +19,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt"
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -19,10 +19,10 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"from scipy.special import roots_legendre\n",
|
||||
"from scipy.integrate import quad\n",
|
||||
"import matplotlib.pyplot as plt"
|
||||
"from scipy.special import roots_legendre"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -710,14 +710,14 @@
|
||||
"\n",
|
||||
"for N in range(1, 11):\n",
|
||||
" approx_errors = []\n",
|
||||
" for k in [x for x in range(0, 11, 2)]:\n",
|
||||
" for k in list(range(0, 11, 2)):\n",
|
||||
" I_approx = gauss(lambda x: f(x, k), N)\n",
|
||||
" I_exact = 2 / (k + 1) if k % 2 == 0 else 0\n",
|
||||
" approx_error = np.abs(I_approx - I_exact)\n",
|
||||
" approx_errors.append(approx_error)\n",
|
||||
" print(\n",
|
||||
" \"{:5d} | \".format(N)\n",
|
||||
" + \" \".join(\"{:.3f} \".format(e) for e in approx_errors)\n",
|
||||
" f\"{N:5d} | \"\n",
|
||||
" + \" \".join(f\"{e:.3f} \" for e in approx_errors)\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
@@ -768,14 +768,14 @@
|
||||
"\n",
|
||||
"for N in range(1, 11):\n",
|
||||
" approx_errors = []\n",
|
||||
" for k in [x for x in range(0, 11, 2)]:\n",
|
||||
" for k in list(range(0, 11, 2)):\n",
|
||||
" I_approx = fejer(lambda x: f(x, k), N)\n",
|
||||
" I_exact = 2 / (k + 1) if k % 2 == 0 else 0\n",
|
||||
" approx_error = np.abs(I_approx - I_exact)\n",
|
||||
" approx_errors.append(approx_error)\n",
|
||||
" print(\n",
|
||||
" \"{:5d} | \".format(N)\n",
|
||||
" + \" \".join(\"{:.3f} \".format(e) for e in approx_errors)\n",
|
||||
" f\"{N:5d} | \"\n",
|
||||
" + \" \".join(f\"{e:.3f} \" for e in approx_errors)\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
"%matplotlib inline\n",
|
||||
"%config InlineBackend.figure_format = 'retina'\n",
|
||||
"\n",
|
||||
"import numpy as np # pour les numpy array\n",
|
||||
"import matplotlib.pyplot as plt # librairie graphique"
|
||||
"import matplotlib.pyplot as plt # librairie graphique\n",
|
||||
"import numpy as np # pour les numpy array"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -331,7 +331,8 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"f = lambda x: 1 / (1 + x**2)\n",
|
||||
"def f(x):\n",
|
||||
" return 1 / (1 + x**2)\n",
|
||||
"a, b = -5, 5\n",
|
||||
"xx = np.linspace(a, b, 200)\n",
|
||||
"\n",
|
||||
@@ -372,7 +373,8 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"f = lambda x: 1 / (1 + x**2)\n",
|
||||
"def f(x):\n",
|
||||
" return 1 / (1 + x**2)\n",
|
||||
"a, b = -5, 5\n",
|
||||
"xx = np.linspace(a, b, 200)\n",
|
||||
"\n",
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -20,8 +20,8 @@
|
||||
"%matplotlib inline\n",
|
||||
"%config InlineBackend.figure_format = 'retina'\n",
|
||||
"\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt"
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -63,11 +63,16 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"f1 = lambda x: np.exp(x) - 1 - x\n",
|
||||
"f2 = lambda x: x - np.sin(x)\n",
|
||||
"f3 = lambda x: x + np.sin(x)\n",
|
||||
"f4 = lambda x: x + np.cos(x) - 1\n",
|
||||
"f5 = lambda x: x - np.cos(x) + 1"
|
||||
"def f1(x):\n",
|
||||
" return np.exp(x) - 1 - x\n",
|
||||
"def f2(x):\n",
|
||||
" return x - np.sin(x)\n",
|
||||
"def f3(x):\n",
|
||||
" return x + np.sin(x)\n",
|
||||
"def f4(x):\n",
|
||||
" return x + np.cos(x) - 1\n",
|
||||
"def f5(x):\n",
|
||||
" return x - np.cos(x) + 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -100,8 +100,8 @@
|
||||
],
|
||||
"source": [
|
||||
"%matplotlib inline\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Fonction f définissant l'EDO\n",
|
||||
@@ -189,8 +189,8 @@
|
||||
],
|
||||
"source": [
|
||||
"%matplotlib inline\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Fonction f définissant l'EDO\n",
|
||||
@@ -367,7 +367,7 @@
|
||||
"T = 1\n",
|
||||
"y0 = 1\n",
|
||||
"\n",
|
||||
"for f, uex in zip([f1, f2], [uex1, uex2]):\n",
|
||||
"for f, uex in zip([f1, f2], [uex1, uex2], strict=False):\n",
|
||||
" plt.figure()\n",
|
||||
" t = np.arange(0, 1, 1e-3)\n",
|
||||
" y = uex(t, y0)\n",
|
||||
|
||||
@@ -92,14 +92,14 @@
|
||||
],
|
||||
"source": [
|
||||
"%matplotlib inline\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Fonction F définissant l'EDO\n",
|
||||
"def F(Y):\n",
|
||||
" x = Y[0]\n",
|
||||
" y = Y[1]\n",
|
||||
" Y[0]\n",
|
||||
" Y[1]\n",
|
||||
" A = np.array([[0, 1], [-2, -3]])\n",
|
||||
" return np.dot(A, Y)\n",
|
||||
"\n",
|
||||
@@ -132,7 +132,7 @@
|
||||
"## Représentation des solutions pour chaque valeur de la donnée initiale\n",
|
||||
"tt = np.linspace(-10, 10, 100)\n",
|
||||
"t0 = tt[0]\n",
|
||||
"for x, y in zip([1, -2, 0, 1, 3], [2, -2, -4, -2, 4]):\n",
|
||||
"for x, y in zip([1, -2, 0, 1, 3], [2, -2, -4, -2, 4], strict=False):\n",
|
||||
" sol = uex(tt, t0, [x, y])\n",
|
||||
" plt.plot(sol[0], sol[1], label=f\"$((x0, y0) = ({x}, {y})$\")\n",
|
||||
" plt.scatter(x, y)\n",
|
||||
|
||||
@@ -73,8 +73,8 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%matplotlib inline\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Question 1\n",
|
||||
@@ -746,8 +746,9 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%matplotlib inline\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"# Question 3\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -358,7 +358,7 @@
|
||||
"H, E = np.array(H), np.array(E)\n",
|
||||
"\n",
|
||||
"plt.xlabel(\"$h$\")\n",
|
||||
"plt.ylabel(\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n",
|
||||
"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",
|
||||
@@ -589,7 +589,7 @@
|
||||
" H.append(h)\n",
|
||||
"\n",
|
||||
"plt.xlabel(\"$h$\")\n",
|
||||
"plt.ylabel(\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n",
|
||||
"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",
|
||||
@@ -833,7 +833,7 @@
|
||||
" H.append(h)\n",
|
||||
"\n",
|
||||
"plt.xlabel(\"$h$\")\n",
|
||||
"plt.ylabel(\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n",
|
||||
"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",
|
||||
|
||||
@@ -21,9 +21,10 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import itertools\n",
|
||||
"\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import itertools"
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -139,7 +140,7 @@
|
||||
" return S * 2 / (self.Ntot**2)\n",
|
||||
"\n",
|
||||
" def simuler(self, T=400, move_satisfaits=True):\n",
|
||||
" for t in range(1, int((1 - self.p) * self.M**2 * T)):\n",
|
||||
" for _t in range(1, int((1 - self.p) * self.M**2 * T)):\n",
|
||||
" agents = [\n",
|
||||
" (i, j)\n",
|
||||
" for i, row in enumerate(self.grille)\n",
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"import scipy.stats as stats"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -9,11 +9,11 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import scipy.stats as stats\n",
|
||||
"import scipy.special as sp\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import scipy.optimize as opt"
|
||||
"import numpy as np\n",
|
||||
"import scipy.optimize as opt\n",
|
||||
"import scipy.special as sp\n",
|
||||
"import scipy.stats as stats"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"from scipy.integrate import odeint"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -38,10 +38,10 @@
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"from sklearn.neural_network import MLPClassifier\n",
|
||||
"from sklearn.datasets import make_classification\n",
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"from sklearn.metrics import accuracy_score\n",
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"from sklearn.neural_network import MLPClassifier\n",
|
||||
"\n",
|
||||
"accuracies = []\n",
|
||||
"\n",
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"import scipy.stats as stats"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"from scipy.optimize import newton\n",
|
||||
"from scipy.integrate import quad, odeint"
|
||||
"import numpy as np\n",
|
||||
"from scipy.integrate import odeint, quad\n",
|
||||
"from scipy.optimize import newton"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -159,9 +159,11 @@
|
||||
"\n",
|
||||
" for n in range(N - 1):\n",
|
||||
" p1 = f(vt[n], yn[:, n])\n",
|
||||
" F1 = lambda p2: f(vt[n] + h / 3, yn[:, n] + h / 6 * (p1 + p2)) - p2\n",
|
||||
" def F1(p2):\n",
|
||||
" return f(vt[n] + h / 3, yn[:, n] + h / 6 * (p1 + p2)) - p2\n",
|
||||
" p2 = newton(F1, yn[:, n], fprime=None, tol=tol, maxiter=itmax)\n",
|
||||
" F2 = lambda yn1: yn[:, n] + h / 4 * (3 * p2 + f(vt[n + 1], yn1)) - yn1\n",
|
||||
" def F2(yn1):\n",
|
||||
" return yn[:, n] + h / 4 * (3 * p2 + f(vt[n + 1], yn1)) - yn1\n",
|
||||
" yn[:, n + 1] = newton(F2, yn[:, n], fprime=None, tol=tol, maxiter=itmax)\n",
|
||||
" return yn"
|
||||
]
|
||||
@@ -392,7 +394,7 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "base",
|
||||
"display_name": "studies",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
@@ -406,7 +408,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.2"
|
||||
"version": "3.13.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -64,7 +64,8 @@
|
||||
" return b, iter\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"f = lambda x: np.tanh(x)\n",
|
||||
"def f(x):\n",
|
||||
" return np.tanh(x)\n",
|
||||
"aL, aR = -20, 3\n",
|
||||
"print(dichotomy(f, aL, aR))"
|
||||
]
|
||||
@@ -132,9 +133,11 @@
|
||||
" return x0, iter\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"f = lambda x: np.log(np.exp(x) + np.exp(-x))\n",
|
||||
"def f(x):\n",
|
||||
" return np.log(np.exp(x) + np.exp(-x))\n",
|
||||
"x0 = 1.8\n",
|
||||
"df = lambda x: (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))\n",
|
||||
"def df(x):\n",
|
||||
" return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))\n",
|
||||
"print(Newton(f, df, x0))"
|
||||
]
|
||||
},
|
||||
@@ -183,7 +186,8 @@
|
||||
" return x0, iter\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"f = lambda x: np.log(np.exp(x) + np.exp(-x))\n",
|
||||
"def f(x):\n",
|
||||
" return np.log(np.exp(x) + np.exp(-x))\n",
|
||||
"xx = [(1, 1.9), (1, 2.3), (1, 2.4)]\n",
|
||||
"\n",
|
||||
"for x0, x1 in xx:\n",
|
||||
@@ -259,8 +263,10 @@
|
||||
" return x0, iter\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"f = lambda x: np.log(np.exp(x) + np.exp(-x))\n",
|
||||
"df = lambda x: (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))\n",
|
||||
"def f(x):\n",
|
||||
" return np.log(np.exp(x) + np.exp(-x))\n",
|
||||
"def df(x):\n",
|
||||
" return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))\n",
|
||||
"print(DichotomyNewton(f, df, -20, 3))"
|
||||
]
|
||||
},
|
||||
@@ -308,7 +314,8 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"u = lambda x: np.sqrt((6 - x) ** 2 + 4)\n",
|
||||
"def u(x):\n",
|
||||
" return np.sqrt((6 - x) ** 2 + 4)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def objective_function(x):\n",
|
||||
|
||||
@@ -39,8 +39,8 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def generate_thetas(n):\n",
|
||||
@@ -211,9 +211,9 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"from scipy.optimize import minimize\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def polygon_perimeter(theta, n):\n",
|
||||
|
||||
@@ -27,9 +27,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import yfinance as yf\n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import numpy as np"
|
||||
"import yfinance as yf"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -13,9 +13,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import yfinance as yf\n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import numpy as np"
|
||||
"import yfinance as yf"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -259,7 +259,7 @@
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"# Import part of a library\n",
|
||||
"from scipy.stats import norm, multivariate_normal"
|
||||
"from scipy.stats import multivariate_normal, norm"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -32,8 +32,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"rng = np.random.default_rng(seed=42)\n",
|
||||
"size = 100\n",
|
||||
|
||||
@@ -124,7 +124,9 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": "## 1. K-NN classification for `Iris` <a class=\"anchor\" id=\"chapter1\"></a>"
|
||||
"source": [
|
||||
"## 1. K-NN classification for `Iris` <a class=\"anchor\" id=\"chapter1\"></a>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {
|
||||
@@ -331,7 +333,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-02-07T16:32:18.698079Z",
|
||||
@@ -355,8 +357,9 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# np.argsort\n",
|
||||
"from collections import Counter\n",
|
||||
"\n",
|
||||
"# np.argsort\n",
|
||||
"distance_ex = np.array([4, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 0.5, 0.2])\n",
|
||||
"print(\n",
|
||||
" \"The indices where the 4 smallest digits are located are \\n\",\n",
|
||||
@@ -367,9 +370,6 @@
|
||||
"print(\"\\n\")\n",
|
||||
"\n",
|
||||
"# counter.most_common()\n",
|
||||
"\n",
|
||||
"from collections import Counter\n",
|
||||
"\n",
|
||||
"print(\n",
|
||||
" \"In 'aabbbbccccccc', the frequencies of the letters are : \\n\",\n",
|
||||
" Counter(\"aabbbbccccccc\").most_common(),\n",
|
||||
@@ -1274,9 +1274,10 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from itertools import product\n",
|
||||
"\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"from itertools import product\n",
|
||||
"from sklearn.neighbors import KNeighborsClassifier"
|
||||
]
|
||||
},
|
||||
@@ -1411,7 +1412,7 @@
|
||||
"f, axarr = plt.subplots(2, 3, sharex=\"col\", sharey=\"row\", figsize=(15, 12))\n",
|
||||
"\n",
|
||||
"for idx, clf, tt in zip(\n",
|
||||
" product([0, 1, 2], [0, 1, 2]), KNNs, [f\"KNN (k={k})\" for k in nb_neighbors]\n",
|
||||
" product([0, 1, 2], [0, 1, 2]), KNNs, [f\"KNN (k={k})\" for k in nb_neighbors], strict=False\n",
|
||||
"):\n",
|
||||
" Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])\n",
|
||||
" Z = Z.reshape(xx.shape)\n",
|
||||
|
||||
@@ -34,8 +34,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"from sklearn.linear_model import LogisticRegression"
|
||||
]
|
||||
},
|
||||
@@ -41034,8 +41034,8 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"from sklearn import datasets\n",
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"\n",
|
||||
"iris = datasets.load_iris(as_frame=True)\n",
|
||||
"\n",
|
||||
|
||||
@@ -48,7 +48,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-03-26T10:34:01.222808Z",
|
||||
@@ -433,9 +433,9 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd # dataframes are in pandas\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"hitters = pd.read_csv(\"data/Hitters.csv\", index_col=\"Name\")\n",
|
||||
"\n",
|
||||
@@ -896,11 +896,11 @@
|
||||
"source": [
|
||||
"# Hint for Question (4) :\n",
|
||||
"ex = pd.DataFrame(\n",
|
||||
" dict(\n",
|
||||
" nom=[\"Alice\", \"Nicolas\", \"Jean\"],\n",
|
||||
" age=[19, np.NaN, np.NaN],\n",
|
||||
" exam=[15, 14, np.NaN],\n",
|
||||
" )\n",
|
||||
" {\n",
|
||||
" \"nom\": [\"Alice\", \"Nicolas\", \"Jean\"],\n",
|
||||
" \"age\": [19, np.NaN, np.NaN],\n",
|
||||
" \"exam\": [15, 14, np.NaN],\n",
|
||||
" }\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"data : \\n\", ex)\n",
|
||||
@@ -2545,7 +2545,7 @@
|
||||
"\n",
|
||||
"MSEs = []\n",
|
||||
"for name, estimator in zip(\n",
|
||||
" [\"LassoCV\", \"LassoBIC\", \"RidgeCV\", \"OLS\"], [lassoCV, lassoBIC, ridgeCV, linReg]\n",
|
||||
" [\"LassoCV\", \"LassoBIC\", \"RidgeCV\", \"OLS\"], [lassoCV, lassoBIC, ridgeCV, linReg], strict=False\n",
|
||||
"):\n",
|
||||
" y_pred = estimator.predict(Xtest)\n",
|
||||
" MSE = mean_squared_error(Ytest, y_pred)\n",
|
||||
|
||||
@@ -32,9 +32,9 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt"
|
||||
"import pandas as pd"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -84,8 +84,7 @@
|
||||
"type": "unknown"
|
||||
}
|
||||
],
|
||||
"conversionMethod": "pd.DataFrame",
|
||||
"ref": "37d5b76b-9fed-490f-9dd5-20a98409d9ca",
|
||||
"ref": "5aa01ea5-52e9-47bd-bf16-02eda59eafb2",
|
||||
"rows": [
|
||||
[
|
||||
"0",
|
||||
@@ -294,8 +293,7 @@
|
||||
"type": "unknown"
|
||||
}
|
||||
],
|
||||
"conversionMethod": "pd.DataFrame",
|
||||
"ref": "7dc949eb-d6ef-4d5f-969f-c852d588c859",
|
||||
"ref": "fdbea167-a638-4d3f-8877-210d50fec511",
|
||||
"rows": [
|
||||
[
|
||||
"0",
|
||||
@@ -491,8 +489,7 @@
|
||||
"type": "integer"
|
||||
}
|
||||
],
|
||||
"conversionMethod": "pd.DataFrame",
|
||||
"ref": "6f54a3b7-bd30-4a68-b39b-4220633acbfc",
|
||||
"ref": "5b56b570-be86-4796-82f4-bd777c0f3302",
|
||||
"rows": [
|
||||
[
|
||||
"0",
|
||||
@@ -728,8 +725,7 @@
|
||||
"type": "integer"
|
||||
}
|
||||
],
|
||||
"conversionMethod": "pd.DataFrame",
|
||||
"ref": "dc527433-ad4a-4862-bddf-ed1bfa01897a",
|
||||
"ref": "af40f60c-6cb1-4d32-bdfe-3ea091985909",
|
||||
"rows": [
|
||||
[
|
||||
"0",
|
||||
@@ -930,8 +926,7 @@
|
||||
"type": "integer"
|
||||
}
|
||||
],
|
||||
"conversionMethod": "pd.DataFrame",
|
||||
"ref": "01a11b3a-e783-4b35-8867-c7a57df85078",
|
||||
"ref": "74c5f61c-9b82-4dc6-991b-6a173848eccb",
|
||||
"rows": [
|
||||
[
|
||||
"2",
|
||||
@@ -1205,7 +1200,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -1245,7 +1240,7 @@
|
||||
"\n",
|
||||
"print(\n",
|
||||
" \"The vocabulary arranged in alphabetical order : \",\n",
|
||||
" sorted(list(vec.vocabulary_.keys())),\n",
|
||||
" sorted(vec.vocabulary_.keys()),\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# 2. Displaying the vectors :\n",
|
||||
@@ -1341,7 +1336,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 32,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -2190,7 +2185,7 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": ".venv",
|
||||
"display_name": "studies",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
@@ -2204,7 +2199,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.9"
|
||||
"version": "3.13.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -80,7 +80,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"id": "5260add2-2092-4849-b39b-0b4416d60275",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
@@ -91,8 +91,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import tensorflow as tf\n",
|
||||
"\n",
|
||||
"tf.keras.utils.set_random_seed(42)\n",
|
||||
"fashion_mnist = tf.keras.datasets.fashion_mnist\n",
|
||||
@@ -2244,7 +2244,7 @@
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": ".venv",
|
||||
"display_name": "studies",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
@@ -2258,7 +2258,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.9"
|
||||
"version": "3.13.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 154,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -41,9 +41,8 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"np.random.seed(12)\n",
|
||||
"num_observations = 400\n",
|
||||
@@ -1554,7 +1553,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 172,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -1574,7 +1573,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.metrics import confusion_matrix, accuracy_score\n",
|
||||
"from sklearn.metrics import accuracy_score, confusion_matrix\n",
|
||||
"\n",
|
||||
"print(accuracy_score(cluster_to_label, labels))\n",
|
||||
"print(confusion_matrix(cluster_to_label, labels))"
|
||||
|
||||
@@ -6,6 +6,7 @@ readme = "README.md"
|
||||
requires-python = ">=3.12"
|
||||
dependencies = [
|
||||
"ipykernel>=6.29.5",
|
||||
"keras>=3.11.3",
|
||||
"matplotlib>=3.10.1",
|
||||
"numpy>=2.2.5",
|
||||
"opencv-python>=4.11.0.86",
|
||||
|
||||
2
uv.lock
generated
2
uv.lock
generated
@@ -1249,6 +1249,7 @@ version = "0.1.0"
|
||||
source = { virtual = "." }
|
||||
dependencies = [
|
||||
{ name = "ipykernel" },
|
||||
{ name = "keras" },
|
||||
{ name = "matplotlib" },
|
||||
{ name = "numpy" },
|
||||
{ name = "opencv-python" },
|
||||
@@ -1267,6 +1268,7 @@ dev = [
|
||||
[package.metadata]
|
||||
requires-dist = [
|
||||
{ name = "ipykernel", specifier = ">=6.29.5" },
|
||||
{ name = "keras", specifier = ">=3.11.3" },
|
||||
{ name = "matplotlib", specifier = ">=3.10.1" },
|
||||
{ name = "numpy", specifier = ">=2.2.5" },
|
||||
{ name = "opencv-python", specifier = ">=4.11.0.86" },
|
||||
|
||||
Reference in New Issue
Block a user