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:
2025-09-01 16:14:53 +02:00
parent dfee405ea0
commit 8cf328e18a
31 changed files with 177 additions and 156 deletions

View File

@@ -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"
]
},
{

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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,

View File

@@ -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,

View File

@@ -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))"