mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-01-14 15:54:13 +01:00
Ajout de la séparation des données en ensembles d'apprentissage et de test, et implémentation de la recherche de grille pour les hyperparamètres du modèle Random Forest.
This commit is contained in:
@@ -3598,7 +3598,14 @@
|
||||
"id": "d9342ad6",
|
||||
"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",
|
||||
@@ -3618,7 +3625,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 79,
|
||||
"execution_count": null,
|
||||
"id": "6d58dbc2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -3636,9 +3643,9 @@
|
||||
"RMSE_scores = []\n",
|
||||
"\n",
|
||||
"# Hyperparamètres à tester\n",
|
||||
"n_estimators_values = [] #Complétez ici par les paramètres à tester\n",
|
||||
"max_depth_values = [] #Complétez ici par les paramètres à tester\n",
|
||||
"min_samples_split_values = [] #Complétez ici par les paramètres à tester\n",
|
||||
"n_estimators_values = []\n",
|
||||
"max_depth_values = []\n",
|
||||
"min_samples_split_values = []\n",
|
||||
"\n",
|
||||
"# Liste pour sauveagrder les meilleurs résultats\n",
|
||||
"best_score = np.inf\n",
|
||||
@@ -3651,17 +3658,59 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 80,
|
||||
"execution_count": 87,
|
||||
"id": "47da5172",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#Complétez ici avec votre code"
|
||||
"#Complétez ici avec votre code\n",
|
||||
"for n_estimators in n_estimators_values:\n",
|
||||
" for max_depth in max_depth_values:\n",
|
||||
" for min_samples_split in min_samples_split_values:\n",
|
||||
" rf_regressor = RandomForestRegressor(\n",
|
||||
" n_estimators=n_estimators,\n",
|
||||
" max_depth=max_depth,\n",
|
||||
" min_samples_split=min_samples_split,\n",
|
||||
" random_state=42\n",
|
||||
" )\n",
|
||||
" MAE_scores = []\n",
|
||||
" MSE_scores = []\n",
|
||||
" RMSE_scores = []\n",
|
||||
"\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",
|
||||
" avg_mae = np.mean(MAE_scores)\n",
|
||||
" avg_mse = np.mean(MSE_scores)\n",
|
||||
" avg_rmse = np.mean(RMSE_scores)\n",
|
||||
"\n",
|
||||
" if avg_rmse < best_score:\n",
|
||||
" best_score = avg_rmse\n",
|
||||
" best_params = {\n",
|
||||
" 'n_estimators': n_estimators,\n",
|
||||
" 'max_depth': max_depth,\n",
|
||||
" 'min_samples_split': min_samples_split\n",
|
||||
" }\n",
|
||||
" MAE_best_score = MAE_scores\n",
|
||||
" MSE_best_score = MSE_scores\n",
|
||||
" RMSE_best_score = RMSE_scores"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 81,
|
||||
"execution_count": 88,
|
||||
"id": "d4936c46",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
|
||||
Reference in New Issue
Block a user