|
import pandas as pd |
|
import joblib |
|
|
|
|
|
model = joblib.load("model.joblib") |
|
|
|
def preprocess_and_predict(input_json): |
|
""" |
|
Fonction pour traiter les données JSON entrantes, calculer les champs dérivés, |
|
et prédire à l'aide d'un modèle scikit-learn. |
|
|
|
Args: |
|
input_json (dict): Données simplifiées fournies par l'utilisateur. Exemple : |
|
{ |
|
"puiss_admin_98": 7, |
|
"conso_urb": 5.6, |
|
"conso_exurb": 4.3, |
|
"masse_ordma_max": 1500, |
|
"marque": "BMW", |
|
"typ_boite": "A 5", |
|
"champ_v9": 715/2007*195/2013EURO5, |
|
"carrosserie": "COUPE", |
|
"gamme": "LUXE" |
|
} |
|
|
|
Returns: |
|
dict: Prédiction du modèle. |
|
""" |
|
|
|
|
|
marque_mapping = ["BMW", "MERCEDES", "VOLKSWAGEN"] |
|
typ_boite_mapping = ["A 5", "A 6", "A 7", "A 8", "M 5", "M 6"] |
|
carrosserie_mapping = ["BREAK", "COUPE", "MINIBUS", "TS TERRAINS/CHEMINS"] |
|
gamme_mapping = ["INFERIEURE", "LUXE", "MOY-INFERIEURE", "MOY-SUPER", "SUPERIEURE"] |
|
champ_v9_mapping = ["\"715/2007*195/2013EURO5", "\"715/2007*195/2013EURO6", "\"715/2007*566/2011EURO5", "\"715/2007*630/2012EURO5", "715/2007*195/2013EURO5", "715/2007*630/2012EURO5", "715/2007*692/2008EURO5"] |
|
|
|
|
|
processed_data = { |
|
"puiss_admin_98": input_json.get("puiss_admin_98", 0), |
|
"conso_urb": input_json.get("conso_urb", 0.0), |
|
"conso_exurb": input_json.get("conso_exurb", 0.0), |
|
"masse_ordma_max": input_json.get("masse_ordma_max", 0.0) |
|
} |
|
|
|
|
|
for marque in marque_mapping: |
|
processed_data[f"lib_mrq_{marque}"] = 1 if input_json.get("marque") == marque else 0 |
|
|
|
|
|
for typ_boite in typ_boite_mapping: |
|
processed_data[f"typ_boite_nb_rapp_{typ_boite}"] = 1 if input_json.get("typ_boite") == typ_boite else 0 |
|
|
|
|
|
for champ_v9 in champ_v9_mapping: |
|
processed_data[f"champ_v9_{champ_v9}"] = 1 if input_json.get("champ_v9") == champ_v9 else 0 |
|
|
|
|
|
for carrosserie in carrosserie_mapping: |
|
processed_data[f"Carrosserie_{carrosserie}"] = 1 if input_json.get("carrosserie") == carrosserie else 0 |
|
|
|
|
|
for gamme in gamme_mapping: |
|
processed_data[f"gamme_{gamme}"] = 1 if input_json.get("gamme") == gamme else 0 |
|
|
|
|
|
input_dataframe = pd.DataFrame([processed_data]) |
|
|
|
|
|
prediction = model.predict(input_dataframe) |
|
return {"prediction": prediction.tolist()} |
|
|