Skip to content
Snippets Groups Projects
Commit 9e422955 authored by Mohamed Sebabti's avatar Mohamed Sebabti
Browse files

grid search rn

parent b2365d4c
No related branches found
No related tags found
No related merge requests found
...@@ -28,26 +28,40 @@ def train_mlp(df): ...@@ -28,26 +28,40 @@ def train_mlp(df):
# 2. Séparation train/test # 2. Séparation train/test
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(X, y, test_size=0.2, random_state=42)
# 3. Normalisation des features (obligatoire pour MLP) # 3. Normalisation des features
scaler = StandardScaler() scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train) X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test) X_test_scaled = scaler.transform(X_test)
# 4. Définition et entraînement du modèle MLP # 4. Définition des hyperparamètres à tester
mlp = MLPRegressor(hidden_layer_sizes=(100, 50), activation='relu', solver='adam', max_iter=500, random_state=42) param_grid = {
mlp.fit(X_train_scaled, y_train) 'hidden_layer_sizes': [(50,), (100,), (100, 50), (100, 100)],
'activation': ['relu', 'tanh'],
'solver': ['adam', 'sgd'],
'learning_rate_init': [0.001, 0.01, 0.1],
'max_iter': [500]
}
# 5. Prédictions # 5. Recherche des meilleurs hyperparamètres avec GridSearchCV
y_pred = mlp.predict(X_test_scaled) mlp = MLPRegressor(random_state=42)
grid_search = GridSearchCV(mlp, param_grid, cv=3, scoring='r2', n_jobs=-1, verbose=2)
grid_search.fit(X_train_scaled, y_train)
# 6. Évaluation du modèle # 6. Affichage des meilleurs paramètres
print("Meilleurs paramètres :", grid_search.best_params_)
# 7. Prédictions avec le meilleur modèle
best_mlp = grid_search.best_estimator_
y_pred = best_mlp.predict(X_test_scaled)
# 8. Évaluation du modèle
mae = mean_absolute_error(y_test, y_pred) mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred)) rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2 = r2_score(y_test, y_pred) r2 = r2_score(y_test, y_pred)
print(f"📊 MLPRegressor - MAE: {mae:.2f}, RMSE: {rmse:.2f}, R²: {r2:.3f}") print(f"📊 MLPRegressor Optimisé - MAE: {mae:.2f}, RMSE: {rmse:.2f}, R²: {r2:.3f}")
# 7. Ajout des prédictions au DataFrame original # 9. Ajout des prédictions au DataFrame original
df.loc[X_test.index, 'pred_mlp'] = y_pred df.loc[X_test.index, 'pred_mlp'] = y_pred
print(df.head(40)) print(df.head(40))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment