Spaces:
Sleeping
Sleeping
Upload processing.py
Browse files- processing.py +18 -19
processing.py
CHANGED
|
@@ -63,42 +63,41 @@ def map_statut_expert(p2):
|
|
| 63 |
# --- 3. PRÉPARATION DES DONNÉES (La version robuste) ---
|
| 64 |
|
| 65 |
def prepare_input(data):
|
| 66 |
-
# 1. On définit la liste EXACTE
|
| 67 |
-
# On
|
| 68 |
-
|
| 69 |
-
model_columns = [c for c in FEATURES if c not in cols_to_exclude]
|
| 70 |
|
| 71 |
-
# Sécurité :
|
| 72 |
-
if
|
| 73 |
-
|
| 74 |
|
| 75 |
-
# 2. Création du
|
| 76 |
-
df = pd.DataFrame(0.0, index=[0], columns=
|
| 77 |
-
|
| 78 |
-
# 3. Remplissage
|
| 79 |
-
# On utilise age_estime venant du Streamlit pour remplir age_au_diagnostic
|
| 80 |
df['age_au_diagnostic'] = float(data.get('age_estime', 0))
|
| 81 |
df['Tranche_effectif_num'] = float(data.get('Tranche_effectif_num', 0))
|
| 82 |
df['is_ess'] = int(data.get('is_ess', 0))
|
| 83 |
|
| 84 |
-
#
|
| 85 |
code_dep = str(data.get('code_departement', '')).strip().upper()
|
| 86 |
df['risque_departemental'] = float(DEP_RISK_MAP.get(code_dep, 0.05))
|
| 87 |
|
| 88 |
-
#
|
| 89 |
code_ape = str(data.get('code_ape', '')).zfill(2)
|
| 90 |
section_name = APE_SECTION_MAP.get(code_ape)
|
| 91 |
if section_name:
|
| 92 |
col_ape = f"APE_{section_name}"
|
| 93 |
if col_ape in df.columns:
|
| 94 |
df[col_ape] = 1.0
|
| 95 |
-
|
| 96 |
df['APE_Autres_Secteurs'] = 1.0
|
| 97 |
-
|
| 98 |
-
#
|
| 99 |
cj_prefix = str(data.get('categorie_juridique', ''))[:4]
|
| 100 |
col_cj = f"CJ_{cj_prefix}"
|
| 101 |
if col_cj in df.columns:
|
| 102 |
df[col_cj] = 1.0
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
| 63 |
# --- 3. PRÉPARATION DES DONNÉES (La version robuste) ---
|
| 64 |
|
| 65 |
def prepare_input(data):
|
| 66 |
+
# 1. On définit la liste EXACTE que XGBoost a apprise (ordre de l'erreur 500)
|
| 67 |
+
# On retire SIREN et Dénomination car ils ne sont PAS dans le modèle
|
| 68 |
+
target_columns = [c for c in FEATURES if c not in ["SIREN", "Dénomination de l'unité légale"]]
|
|
|
|
| 69 |
|
| 70 |
+
# Sécurité absolue : on s'assure que 'age_au_diagnostic' est bien là
|
| 71 |
+
if "age_au_diagnostic" not in target_columns:
|
| 72 |
+
target_columns.append("age_au_diagnostic")
|
| 73 |
|
| 74 |
+
# 2. Création du DF avec l'ordre imposé
|
| 75 |
+
df = pd.DataFrame(0.0, index=[0], columns=target_columns)
|
| 76 |
+
|
| 77 |
+
# 3. Remplissage par NOM (Pandas s'occupera de les mettre au bon index)
|
|
|
|
| 78 |
df['age_au_diagnostic'] = float(data.get('age_estime', 0))
|
| 79 |
df['Tranche_effectif_num'] = float(data.get('Tranche_effectif_num', 0))
|
| 80 |
df['is_ess'] = int(data.get('is_ess', 0))
|
| 81 |
|
| 82 |
+
# Département
|
| 83 |
code_dep = str(data.get('code_departement', '')).strip().upper()
|
| 84 |
df['risque_departemental'] = float(DEP_RISK_MAP.get(code_dep, 0.05))
|
| 85 |
|
| 86 |
+
# Secteur APE
|
| 87 |
code_ape = str(data.get('code_ape', '')).zfill(2)
|
| 88 |
section_name = APE_SECTION_MAP.get(code_ape)
|
| 89 |
if section_name:
|
| 90 |
col_ape = f"APE_{section_name}"
|
| 91 |
if col_ape in df.columns:
|
| 92 |
df[col_ape] = 1.0
|
| 93 |
+
else:
|
| 94 |
df['APE_Autres_Secteurs'] = 1.0
|
| 95 |
+
|
| 96 |
+
# Forme Juridique
|
| 97 |
cj_prefix = str(data.get('categorie_juridique', ''))[:4]
|
| 98 |
col_cj = f"CJ_{cj_prefix}"
|
| 99 |
if col_cj in df.columns:
|
| 100 |
df[col_cj] = 1.0
|
| 101 |
+
|
| 102 |
+
# 4. TRÈS IMPORTANT : On retourne le DMatrix avec les colonnes dans l'ordre de target_columns
|
| 103 |
+
return xgb.DMatrix(df[target_columns])
|