Refactor code in 2025_TP_3_M2_ISF.ipynb:

- Updated execution counts for multiple code cells to maintain consistency.
- Removed redundant imports and organized import statements.
- Improved formatting for better readability in train-test split section.
- Added markdown explanations for model performance metrics (MAE, RMSE).
- Enhanced cross-validation training loop with detailed output for each fold's metrics.
This commit is contained in:
2025-10-13 19:58:58 +02:00
parent 047f30def1
commit a0b0a9f8bd

View File

@@ -46,7 +46,7 @@
},
{
"cell_type": "code",
"execution_count": 157,
"execution_count": 56,
"id": "97d58527",
"metadata": {},
"outputs": [],
@@ -61,18 +61,17 @@
"sns.set()\n",
"import plotly.express as px\n",
"import plotly.graph_objects as gp\n",
"import sklearn.metrics as metrics\n",
"import sklearn.preprocessing as preproc\n",
"\n",
"#Statistiques\n",
"from scipy.stats import chi2_contingency\n",
"from sklearn import metrics\n",
"\n",
"# Machine Learning\n",
"from sklearn.cluster import KMeans\n",
"import sklearn.metrics as metrics\n",
"from sklearn.ensemble import RandomForestRegressor\n",
"from sklearn.model_selection import KFold, train_test_split\n",
"from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor"
"from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor\n"
]
},
{
@@ -101,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count": 158,
"execution_count": 57,
"id": "c9597b48",
"metadata": {},
"outputs": [],
@@ -120,7 +119,7 @@
},
{
"cell_type": "code",
"execution_count": 159,
"execution_count": 58,
"id": "8051b5f4",
"metadata": {},
"outputs": [],
@@ -164,7 +163,7 @@
},
{
"cell_type": "code",
"execution_count": 160,
"execution_count": 59,
"id": "c427a4b8",
"metadata": {},
"outputs": [
@@ -174,7 +173,7 @@
"(824, 14)"
]
},
"execution_count": 160,
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
@@ -201,7 +200,7 @@
},
{
"cell_type": "code",
"execution_count": 161,
"execution_count": 60,
"id": "c8fd3ee1",
"metadata": {},
"outputs": [
@@ -285,7 +284,7 @@
"type": "float"
}
],
"ref": "e80a8f38-8160-41fb-bbfa-ae1f7b39de11",
"ref": "8fcd0abc-8334-4a0d-96b7-b6d7e17b3fb7",
"rows": [
[
"count",
@@ -761,7 +760,7 @@
"max NaN 83421.850000 "
]
},
"execution_count": 161,
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
@@ -796,7 +795,7 @@
},
{
"cell_type": "code",
"execution_count": 162,
"execution_count": 61,
"id": "1b156435",
"metadata": {},
"outputs": [
@@ -806,7 +805,7 @@
"(824, 13)"
]
},
"execution_count": 162,
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
@@ -818,7 +817,7 @@
},
{
"cell_type": "code",
"execution_count": 163,
"execution_count": 62,
"id": "0ef0fcc0",
"metadata": {},
"outputs": [],
@@ -854,7 +853,7 @@
},
{
"cell_type": "code",
"execution_count": 164,
"execution_count": 63,
"id": "e130aae5",
"metadata": {},
"outputs": [],
@@ -864,7 +863,7 @@
},
{
"cell_type": "code",
"execution_count": 165,
"execution_count": 64,
"id": "c39e2ad0",
"metadata": {},
"outputs": [
@@ -1812,7 +1811,7 @@
},
{
"cell_type": "code",
"execution_count": 166,
"execution_count": 65,
"id": "a16215ab",
"metadata": {},
"outputs": [],
@@ -1822,7 +1821,7 @@
},
{
"cell_type": "code",
"execution_count": 167,
"execution_count": 66,
"id": "532ca6c4",
"metadata": {},
"outputs": [
@@ -2765,7 +2764,7 @@
},
{
"cell_type": "code",
"execution_count": 168,
"execution_count": 67,
"id": "b8530717",
"metadata": {},
"outputs": [],
@@ -2786,7 +2785,7 @@
},
{
"cell_type": "code",
"execution_count": 169,
"execution_count": 68,
"id": "4ff3847d",
"metadata": {},
"outputs": [],
@@ -2815,7 +2814,7 @@
},
{
"cell_type": "code",
"execution_count": 170,
"execution_count": 69,
"id": "6a1c7907",
"metadata": {},
"outputs": [],
@@ -2823,7 +2822,9 @@
"X = data_model_preprocessed = vars_numeriques_scaled.merge(vars_categorielles_enc, left_index=True, right_index=True) # type: ignore\n",
"Y = data_model[\"CM\"]\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42)"
"X_train, X_test, y_train, y_test = train_test_split(\n",
" X, Y, test_size=0.2, random_state=42\n",
")"
]
},
{
@@ -2844,14 +2845,14 @@
},
{
"cell_type": "code",
"execution_count": 171,
"execution_count": 70,
"id": "053e013c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>#sk-container-id-4 {\n",
"<style>#sk-container-id-3 {\n",
" /* Definition of color scheme common for light and dark mode */\n",
" --sklearn-color-text: #000;\n",
" --sklearn-color-text-muted: #666;\n",
@@ -2882,15 +2883,15 @@
" }\n",
"}\n",
"\n",
"#sk-container-id-4 {\n",
"#sk-container-id-3 {\n",
" color: var(--sklearn-color-text);\n",
"}\n",
"\n",
"#sk-container-id-4 pre {\n",
"#sk-container-id-3 pre {\n",
" padding: 0;\n",
"}\n",
"\n",
"#sk-container-id-4 input.sk-hidden--visually {\n",
"#sk-container-id-3 input.sk-hidden--visually {\n",
" border: 0;\n",
" clip: rect(1px 1px 1px 1px);\n",
" clip: rect(1px, 1px, 1px, 1px);\n",
@@ -2902,7 +2903,7 @@
" width: 1px;\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-dashed-wrapped {\n",
"#sk-container-id-3 div.sk-dashed-wrapped {\n",
" border: 1px dashed var(--sklearn-color-line);\n",
" margin: 0 0.4em 0.5em 0.4em;\n",
" box-sizing: border-box;\n",
@@ -2910,7 +2911,7 @@
" background-color: var(--sklearn-color-background);\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-container {\n",
"#sk-container-id-3 div.sk-container {\n",
" /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
" but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
" so we also need the `!important` here to be able to override the\n",
@@ -2920,7 +2921,7 @@
" position: relative;\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-text-repr-fallback {\n",
"#sk-container-id-3 div.sk-text-repr-fallback {\n",
" display: none;\n",
"}\n",
"\n",
@@ -2936,14 +2937,14 @@
"\n",
"/* Parallel-specific style estimator block */\n",
"\n",
"#sk-container-id-4 div.sk-parallel-item::after {\n",
"#sk-container-id-3 div.sk-parallel-item::after {\n",
" content: \"\";\n",
" width: 100%;\n",
" border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
" flex-grow: 1;\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-parallel {\n",
"#sk-container-id-3 div.sk-parallel {\n",
" display: flex;\n",
" align-items: stretch;\n",
" justify-content: center;\n",
@@ -2951,28 +2952,28 @@
" position: relative;\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-parallel-item {\n",
"#sk-container-id-3 div.sk-parallel-item {\n",
" display: flex;\n",
" flex-direction: column;\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-parallel-item:first-child::after {\n",
"#sk-container-id-3 div.sk-parallel-item:first-child::after {\n",
" align-self: flex-end;\n",
" width: 50%;\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-parallel-item:last-child::after {\n",
"#sk-container-id-3 div.sk-parallel-item:last-child::after {\n",
" align-self: flex-start;\n",
" width: 50%;\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-parallel-item:only-child::after {\n",
"#sk-container-id-3 div.sk-parallel-item:only-child::after {\n",
" width: 0;\n",
"}\n",
"\n",
"/* Serial-specific style estimator block */\n",
"\n",
"#sk-container-id-4 div.sk-serial {\n",
"#sk-container-id-3 div.sk-serial {\n",
" display: flex;\n",
" flex-direction: column;\n",
" align-items: center;\n",
@@ -2990,14 +2991,14 @@
"\n",
"/* Pipeline and ColumnTransformer style (default) */\n",
"\n",
"#sk-container-id-4 div.sk-toggleable {\n",
"#sk-container-id-3 div.sk-toggleable {\n",
" /* Default theme specific background. It is overwritten whether we have a\n",
" specific estimator or a Pipeline/ColumnTransformer */\n",
" background-color: var(--sklearn-color-background);\n",
"}\n",
"\n",
"/* Toggleable label */\n",
"#sk-container-id-4 label.sk-toggleable__label {\n",
"#sk-container-id-3 label.sk-toggleable__label {\n",
" cursor: pointer;\n",
" display: flex;\n",
" width: 100%;\n",
@@ -3010,13 +3011,13 @@
" gap: 0.5em;\n",
"}\n",
"\n",
"#sk-container-id-4 label.sk-toggleable__label .caption {\n",
"#sk-container-id-3 label.sk-toggleable__label .caption {\n",
" font-size: 0.6rem;\n",
" font-weight: lighter;\n",
" color: var(--sklearn-color-text-muted);\n",
"}\n",
"\n",
"#sk-container-id-4 label.sk-toggleable__label-arrow:before {\n",
"#sk-container-id-3 label.sk-toggleable__label-arrow:before {\n",
" /* Arrow on the left of the label */\n",
" content: \"▸\";\n",
" float: left;\n",
@@ -3024,13 +3025,13 @@
" color: var(--sklearn-color-icon);\n",
"}\n",
"\n",
"#sk-container-id-4 label.sk-toggleable__label-arrow:hover:before {\n",
"#sk-container-id-3 label.sk-toggleable__label-arrow:hover:before {\n",
" color: var(--sklearn-color-text);\n",
"}\n",
"\n",
"/* Toggleable content - dropdown */\n",
"\n",
"#sk-container-id-4 div.sk-toggleable__content {\n",
"#sk-container-id-3 div.sk-toggleable__content {\n",
" max-height: 0;\n",
" max-width: 0;\n",
" overflow: hidden;\n",
@@ -3039,12 +3040,12 @@
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-toggleable__content.fitted {\n",
"#sk-container-id-3 div.sk-toggleable__content.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-toggleable__content pre {\n",
"#sk-container-id-3 div.sk-toggleable__content pre {\n",
" margin: 0.2em;\n",
" border-radius: 0.25em;\n",
" color: var(--sklearn-color-text);\n",
@@ -3052,79 +3053,79 @@
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-toggleable__content.fitted pre {\n",
"#sk-container-id-3 div.sk-toggleable__content.fitted pre {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-4 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
"#sk-container-id-3 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
" /* Expand drop-down */\n",
" max-height: 200px;\n",
" max-width: 100%;\n",
" overflow: auto;\n",
"}\n",
"\n",
"#sk-container-id-4 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
"#sk-container-id-3 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
" content: \"▾\";\n",
"}\n",
"\n",
"/* Pipeline/ColumnTransformer-specific style */\n",
"\n",
"#sk-container-id-4 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
"#sk-container-id-3 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
"#sk-container-id-3 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Estimator-specific style */\n",
"\n",
"/* Colorize estimator box */\n",
"#sk-container-id-4 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
"#sk-container-id-3 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
"#sk-container-id-3 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-label label.sk-toggleable__label,\n",
"#sk-container-id-4 div.sk-label label {\n",
"#sk-container-id-3 div.sk-label label.sk-toggleable__label,\n",
"#sk-container-id-3 div.sk-label label {\n",
" /* The background is the default theme color */\n",
" color: var(--sklearn-color-text-on-default-background);\n",
"}\n",
"\n",
"/* On hover, darken the color of the background */\n",
"#sk-container-id-4 div.sk-label:hover label.sk-toggleable__label {\n",
"#sk-container-id-3 div.sk-label:hover label.sk-toggleable__label {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"/* Label box, darken color on hover, fitted */\n",
"#sk-container-id-4 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
"#sk-container-id-3 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Estimator label */\n",
"\n",
"#sk-container-id-4 div.sk-label label {\n",
"#sk-container-id-3 div.sk-label label {\n",
" font-family: monospace;\n",
" font-weight: bold;\n",
" display: inline-block;\n",
" line-height: 1.2em;\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-label-container {\n",
"#sk-container-id-3 div.sk-label-container {\n",
" text-align: center;\n",
"}\n",
"\n",
"/* Estimator-specific */\n",
"#sk-container-id-4 div.sk-estimator {\n",
"#sk-container-id-3 div.sk-estimator {\n",
" font-family: monospace;\n",
" border: 1px dotted var(--sklearn-color-border-box);\n",
" border-radius: 0.25em;\n",
@@ -3134,18 +3135,18 @@
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-estimator.fitted {\n",
"#sk-container-id-3 div.sk-estimator.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"/* on hover */\n",
"#sk-container-id-4 div.sk-estimator:hover {\n",
"#sk-container-id-3 div.sk-estimator:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-4 div.sk-estimator.fitted:hover {\n",
"#sk-container-id-3 div.sk-estimator.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
@@ -3233,7 +3234,7 @@
"\n",
"/* \"?\"-specific style due to the `<a>` HTML tag */\n",
"\n",
"#sk-container-id-4 a.estimator_doc_link {\n",
"#sk-container-id-3 a.estimator_doc_link {\n",
" float: right;\n",
" font-size: 1rem;\n",
" line-height: 1em;\n",
@@ -3248,31 +3249,31 @@
" border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
"}\n",
"\n",
"#sk-container-id-4 a.estimator_doc_link.fitted {\n",
"#sk-container-id-3 a.estimator_doc_link.fitted {\n",
" /* fitted */\n",
" border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-1);\n",
"}\n",
"\n",
"/* On hover */\n",
"#sk-container-id-4 a.estimator_doc_link:hover {\n",
"#sk-container-id-3 a.estimator_doc_link:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-3);\n",
" color: var(--sklearn-color-background);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"#sk-container-id-4 a.estimator_doc_link.fitted:hover {\n",
"#sk-container-id-3 a.estimator_doc_link.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"</style><div id=\"sk-container-id-4\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>DecisionTreeRegressor()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-4\" type=\"checkbox\" checked><label for=\"sk-estimator-id-4\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow\"><div><div>DecisionTreeRegressor</div></div><div><a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.6/modules/generated/sklearn.tree.DecisionTreeRegressor.html\">?<span>Documentation for DecisionTreeRegressor</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></div></label><div class=\"sk-toggleable__content fitted\"><pre>DecisionTreeRegressor()</pre></div> </div></div></div></div>"
"</style><div id=\"sk-container-id-3\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>DecisionTreeRegressor()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" checked><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow\"><div><div>DecisionTreeRegressor</div></div><div><a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.6/modules/generated/sklearn.tree.DecisionTreeRegressor.html\">?<span>Documentation for DecisionTreeRegressor</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></div></label><div class=\"sk-toggleable__content fitted\"><pre>DecisionTreeRegressor()</pre></div> </div></div></div></div>"
],
"text/plain": [
"DecisionTreeRegressor()"
]
},
"execution_count": 171,
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
@@ -3292,7 +3293,7 @@
},
{
"cell_type": "code",
"execution_count": 172,
"execution_count": 71,
"id": "c4ca2cf9",
"metadata": {},
"outputs": [
@@ -3321,7 +3322,7 @@
},
{
"cell_type": "code",
"execution_count": 173,
"execution_count": 72,
"id": "4b739d5b",
"metadata": {},
"outputs": [
@@ -3329,9 +3330,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"MAE: 5950.05\n",
"MSE: 160067768.70\n",
"RMSE: 12651.79\n"
"MAE: 5124.14\n",
"MSE: 84535204.52\n",
"RMSE: 9194.30\n"
]
}
],
@@ -3355,6 +3356,20 @@
"**Question :** que pensez-vous des performances de ce modèle ?"
]
},
{
"cell_type": "markdown",
"id": "bdd7ccd6",
"metadata": {},
"source": [
"*Réponse*: \n",
"\n",
"Erreur Absolue Moyenne (MAE)\n",
"La MAE représente l'écart absolu moyen entre les prédictions du modèle et les valeurs réelles. Une MAE de 5950.05 signifie qu'en moyenne, notre modèle commet une erreur de cette magnitude, dans l'unité de la variable cible. C'est l'indicateur le plus direct de l'erreur de prédiction moyenne.\n",
"\n",
"Racine de l'Erreur Quadratique Moyenne (RMSE)\n",
"La RMSE est la racine carrée de la moyenne des erreurs au carré ($RMSE = \\sqrt{MSE}$). En raison de l'opération de mise au carré, cette métrique est particulièrement sensible aux grandes erreurs. La valeur obtenue est de 12651.79."
]
},
{
"cell_type": "markdown",
"id": "7ecba832",
@@ -3393,11 +3408,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 73,
"id": "ab1e1367",
"metadata": {},
"outputs": [],
"source": []
"source": [
"X = data_model_preprocessed\n",
"Y = data_model[\"CM\"]\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
" X, Y, test_size=0.2, random_state=42\n",
")"
]
},
{
"cell_type": "markdown",
@@ -3417,12 +3439,12 @@
},
{
"cell_type": "code",
"execution_count": 174,
"execution_count": 74,
"id": "b515460e",
"metadata": {},
"outputs": [],
"source": [
"#Initialisation\n",
"# Initialisation\n",
"# Nombre de sous-échantillons pour la cross-validation\n",
"num_splits = 5\n",
"\n",
@@ -3440,20 +3462,56 @@
},
{
"cell_type": "code",
"execution_count": 175,
"execution_count": 75,
"id": "eebb394f",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Validation croisée terminée avec 5 folds\n"
]
}
],
"source": [
"# Entrainement avec cross-validation\n"
"# Entrainement avec cross-validation\n",
"for train_index, val_index in kf.split(X_train):\n",
" X_train_fold, X_val_fold = X_train.iloc[train_index], X_train.iloc[val_index]\n",
" y_train_fold, y_val_fold = y_train.iloc[train_index], y_train.iloc[val_index]\n",
"\n",
" rf_regressor.fit(X_train_fold, y_train_fold)\n",
" y_pred_fold = rf_regressor.predict(X_val_fold)\n",
"\n",
" mae = metrics.mean_absolute_error(y_val_fold, y_pred_fold)\n",
" mse = metrics.mean_squared_error(y_val_fold, y_pred_fold)\n",
" rmse = metrics.root_mean_squared_error(y_val_fold, y_pred_fold)\n",
"\n",
" MAE_scores.append(mae)\n",
" MSE_scores.append(mse)\n",
" RMSE_scores.append(rmse)\n",
"\n",
"print(f\"Validation croisée terminée avec {len(MAE_scores)} folds\")"
]
},
{
"cell_type": "code",
"execution_count": 176,
"execution_count": 76,
"id": "b067126c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fold 1 MAE: 4472.5486946969695\n",
"Fold 2 MAE: 3859.4743234848484\n",
"Fold 3 MAE: 3633.0231541666662\n",
"Fold 4 MAE: 3888.3910715909087\n",
"Fold 5 MAE: 4808.59621832061\n"
]
}
],
"source": [
"# Métriques sur tous les folds\n",
"\n",
@@ -3464,10 +3522,22 @@
},
{
"cell_type": "code",
"execution_count": 177,
"execution_count": 77,
"id": "6597152c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fold 1 MSE: 85464414.44080053\n",
"Fold 2 MSE: 34396997.21755034\n",
"Fold 3 MSE: 55184512.50786593\n",
"Fold 4 MSE: 33191300.80751679\n",
"Fold 5 MSE: 68739370.63588645\n"
]
}
],
"source": [
"#MSE\n",
"for fold, mse in enumerate(MSE_scores, start=1):\n",
@@ -3476,10 +3546,22 @@
},
{
"cell_type": "code",
"execution_count": 178,
"execution_count": 78,
"id": "63ff1c9d",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fold 1 RMSE: 9244.696557529649\n",
"Fold 2 RMSE: 5864.895328780415\n",
"Fold 3 RMSE: 7428.62790210049\n",
"Fold 4 RMSE: 5761.189183451346\n",
"Fold 5 RMSE: 8290.9209763383\n"
]
}
],
"source": [
"#RMSE\n",
"for fold, rmse in enumerate(RMSE_scores, start=1):\n",
@@ -3536,7 +3618,7 @@
},
{
"cell_type": "code",
"execution_count": 179,
"execution_count": 79,
"id": "6d58dbc2",
"metadata": {},
"outputs": [],
@@ -3569,7 +3651,7 @@
},
{
"cell_type": "code",
"execution_count": 180,
"execution_count": 80,
"id": "47da5172",
"metadata": {},
"outputs": [],
@@ -3579,7 +3661,7 @@
},
{
"cell_type": "code",
"execution_count": 181,
"execution_count": 81,
"id": "d4936c46",
"metadata": {},
"outputs": [
@@ -3600,7 +3682,7 @@
},
{
"cell_type": "code",
"execution_count": 182,
"execution_count": 82,
"id": "3215c463",
"metadata": {},
"outputs": [],
@@ -3614,7 +3696,7 @@
},
{
"cell_type": "code",
"execution_count": 183,
"execution_count": 83,
"id": "bb9a5c9b",
"metadata": {},
"outputs": [],
@@ -3626,7 +3708,7 @@
},
{
"cell_type": "code",
"execution_count": 184,
"execution_count": 84,
"id": "0f0768ad",
"metadata": {},
"outputs": [],