|
|
from traning_zone.data_import.data_importation import * |
|
|
import pandas as pd |
|
|
import numpy as np |
|
|
import os |
|
|
|
|
|
import warnings |
|
|
warnings.filterwarnings("ignore") |
|
|
|
|
|
import joblib |
|
|
|
|
|
from sklearn.metrics import balanced_accuracy_score |
|
|
from sklearn.metrics import classification_report , confusion_matrix, accuracy_score, mean_absolute_error |
|
|
|
|
|
import json |
|
|
|
|
|
def trainer(X_train, Y_train, X_test, Y_test, Model, name,classe): |
|
|
|
|
|
try : |
|
|
os.mkdir(f'traning_zone/mini_modèles/{classe}') |
|
|
try : |
|
|
os.mkdir(f'traning_zone/mini_modèles/{classe}/{name}') |
|
|
except FileExistsError: |
|
|
pass |
|
|
except FileExistsError: |
|
|
try : |
|
|
os.mkdir(f'traning_zone/mini_modèles/{classe}/{name}') |
|
|
except FileExistsError: |
|
|
pass |
|
|
|
|
|
classifier = Model.fit(X_train,Y_train) |
|
|
|
|
|
joblib.dump(classifier, open(f"traning_zone/mini_modèles/{classe}/{name}/{name}.pkl", 'wb')) |
|
|
Y_pred = classifier.predict(X_test) |
|
|
|
|
|
score = balanced_accuracy_score(Y_test, Y_pred) |
|
|
|
|
|
with open(f"traning_zone/mini_modèles/{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/{classe}/{name}/classification_report_score.json", "w") as jsonfil: |
|
|
json.dump(scores, jsonfil) |
|
|
|
|
|
print(f'le modèle {name} est terminé') |
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
def trainer_modele(X_train, Y_train, X_test, Y_test, Model, name): |
|
|
|
|
|
try : |
|
|
os.mkdir(f'traning_zone/modèles') |
|
|
try : |
|
|
os.mkdir(f'traning_zone/modèles/{name}') |
|
|
except FileExistsError: |
|
|
pass |
|
|
except FileExistsError: |
|
|
try : |
|
|
os.mkdir(f'traning_zone/modèles/{name}') |
|
|
except FileExistsError: |
|
|
pass |
|
|
|
|
|
classifier = Model.fit(X_train,Y_train) |
|
|
|
|
|
joblib.dump(classifier, open(f"traning_zone/modèles/{name}/{name}.pkl", 'wb')) |
|
|
Y_pred = classifier.predict(X_test) |
|
|
|
|
|
score = balanced_accuracy_score(Y_test, Y_pred) |
|
|
|
|
|
with open(f"traning_zone/modèles/{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/modèles/{name}/classification_report_score.json", "w") as jsonfil: |
|
|
json.dump(scores, jsonfil) |
|
|
|
|
|
print(f'le modèle {name} est terminé') |
|
|
return |
|
|
|