explique moi ligne par lignes ce que fait ce code : from imblearn.pipeline import…
- User
explique moi ligne par lignes ce que fait ce code : from imblearn.pipeline import Pipeline from imblearn.over_sampling import SMOTE from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import GridSearchCV import matplotlib.pyplot as plt
Pipeline : SMOTE puis RandomForest
pipe = Pipeline([ ('smote', SMOTE(random_state=0)), ('rf', RandomForestClassifier(random_state=0, class_weight='balanced')) ])
param_grid = { 'rf__n_estimators': [10, 30, 50, 70, 100, 150, 200, 300] }
grid = GridSearchCV( pipe, param_grid, cv=skf, scoring='f1_macro', n_jobs=-1, return_train_score=True ) grid.fit(X, y)
Affichage des résultats
means = grid.cv_results_['mean_test_score'] stds = grid.cv_results_['std_test_score'] n_estimators_range = param_grid['rf__n_estimators']
plt.figure(figsize=(8,5)) plt.errorbar(n_estimators_range, means, yerr=stds, marker='o', capsize=5) plt.xlabel("n_estimators") plt.ylabel("F1-score macro (CV)") plt.title("GridSearchCV + SMOTE : F1-score macro vs n_estimators (Random Forest)") plt.grid(True) plt.show()
print(f"Meilleur n_estimators : {grid.best_params_['rf__n_estimators']}") print(f"Meilleur F1-score macro : {grid.best_score_:.3f}")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.