File size: 2,093 Bytes
dfcacaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# executives/train_engine.py
import os
import pandas as pd
import joblib
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.multioutput import MultiOutputClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
import warnings

warnings.filterwarnings("ignore")

DATASET_CIBLE = "../datasets/nexus_dataset_v36_combat.csv"
MODEL_OUTPUT = "../pickle_result/nexus_modele_final_V35.pkl"

def entrainer_modele():
    print("==================================================")
    print("🚀 ÉTAPE 3/3 : ENTRAÎNEMENT DU CLASSIFIEUR HYBRIDE V36")
    print("==================================================\n")

    if not os.path.exists(DATASET_CIBLE):
        print(f"❌ ERREUR INFRANCHISSABLE : {DATASET_CIBLE} absent.")
        return

    print("📥 Lecture de la matrice durcie V36...")
    df = pd.read_csv(DATASET_CIBLE).dropna(subset=['texte', 'domaine'])
    
    # Extraction de la donnée d'entraînement
    X = df['texte'].astype(str)
    
    # ---------------------------------------------------------
    # CORRECTION DU TYPAGE MATRICIEL
    # On force la matrice entière Y en String pour empêcher 
    # numpy.unique() de comparer des entiers et du texte.
    # ---------------------------------------------------------
    Y = df[['domaine', 'severite', 'impact', 'cible', 'friction']].astype(str)

    print("⚙️ Initialisation du pipeline TF-IDF Matrix [N-Grams 1-3]...")
    pipeline = Pipeline([
        ('tfidf', TfidfVectorizer(ngram_range=(1, 3), max_features=40000, sublinear_tf=True)),
        ('classifier', MultiOutputClassifier(
            RandomForestClassifier(n_estimators=150, class_weight='balanced', random_state=42, n_jobs=-1)
        ))
    ])

    print("🧠 Calcul des frontières de décision sur l'architecture CPU...")
    pipeline.fit(X, Y)

    os.makedirs(os.path.dirname(MODEL_OUTPUT), exist_ok=True)
    joblib.dump(pipeline, MODEL_OUTPUT)
    print(f"\n✅ BINAIRE SKLEARN SÉRIALISÉ ET MIS À JOUR : {MODEL_OUTPUT}")

if __name__ == "__main__":
    entrainer_modele()