classification / traning_zone /modele_zone /modeles_gridsearch.py
COULIBALY BOURAHIMA
first commit
f1f2665
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score, balanced_accuracy_score, classification_report
from sklearn.model_selection import KFold
import warnings
warnings.filterwarnings("ignore")
import joblib
import os
import json
# dictionnaire des hyperparamètres
parameters = { "GradientBoostingClassifier": {
"learning_rate": [0.01, 0.1, 0.5],
"n_estimators": [50, 100, 200],
"max_depth": [3, 5, 7]
},
"XGBClassifier": {
"learning_rate": [0.01, 0.1, 0.5],
"n_estimators": [50, 100, 200],
"max_depth": [3, 5, 7]
},
"naive_bayes": {
"alpha": [0.1, 0.5, 1.0]
},
"LogisticRegression": {
"penalty": ['l1', 'l2'],
"C": [0.1, 1, 10]
},
"RandomForestClassifier": {
"n_estimators": [50, 100, 200],
"max_depth": [None, 5, 10],
"min_samples_split": [2, 5, 10]
},
"svm": {
"C": [0.1, 1, 10],
"kernel": ['linear', 'rbf'],
"gamma": ['scale', 'auto']
}
}
# fonction de validation croisée
def traning_gridsearch(X_train, Y_train, X_test, Y_test, Model, name,classe, cv = 5):
try :
os.mkdir(f'traning_zone/mini_modèles_w/{classe}')
try :
os.mkdir(f'traning_zone/mini_modèles_w/{classe}/{name}')
except FileExistsError:
pass
except FileExistsError:
try :
os.mkdir(f'traning_zone/mini_modèles_w/{classe}/{name}')
except FileExistsError:
pass
kfold = KFold(n_splits=cv, shuffle=True, random_state=99999)
grid_search = GridSearchCV(Model, parameters[name], scoring="accuracy",cv=kfold)
grid_search.fit(X_train, Y_train)
best_params =grid_search.best_params_
best_score = grid_search.best_score_
best_model = grid_search.best_estimator_
with open(f"traning_zone/mini_modèles_w/{classe}/{name}/best_score.json", "w") as jsonfile:
json.dump(best_score, jsonfile)
with open(f"traning_zone/mini_modèles_w/{classe}/{name}/best_params.json", "w") as jsonfile:
json.dump(best_params, jsonfile)
Y_pred = best_model.predict(X_test)
score = balanced_accuracy_score(Y_test, Y_pred)
with open(f"traning_zone/mini_modèles_w/{classe}/{name}/balanced_accuracy_score_score.json", "w") as jsonfile:
json.dump(score, jsonfile)
scores = classification_report(Y_test, Y_pred)
with open(f"traning_zone/mini_modèles_w/{classe}/{name}/classification_report_score.json", "w") as jsonfil:
json.dump(scores, jsonfil)
return