Spaces:
Sleeping
Sleeping
Delete train_and_save_model.py
Browse files- train_and_save_model.py +0 -115
train_and_save_model.py
DELETED
|
@@ -1,115 +0,0 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
-
from sklearn.model_selection import train_test_split
|
| 4 |
-
from sklearn.preprocessing import StandardScaler, LabelEncoder
|
| 5 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
-
import joblib
|
| 7 |
-
import json
|
| 8 |
-
|
| 9 |
-
print("Chargement et préparation des données...")
|
| 10 |
-
|
| 11 |
-
# Chargement des données
|
| 12 |
-
df = pd.read_csv('/mnt/c/Users/david/Desktop/AIA 2025/BLOC_3_AUTOMATIC_FRAUD_DETECTION/fraudTest.csv', index_col=0)
|
| 13 |
-
|
| 14 |
-
# Feature Engineering
|
| 15 |
-
data = df.copy()
|
| 16 |
-
data['trans_date_trans_time'] = pd.to_datetime(data['trans_date_trans_time'])
|
| 17 |
-
data['hour'] = data['trans_date_trans_time'].dt.hour
|
| 18 |
-
data['day_of_week'] = data['trans_date_trans_time'].dt.dayofweek
|
| 19 |
-
data['day'] = data['trans_date_trans_time'].dt.day
|
| 20 |
-
data['month'] = data['trans_date_trans_time'].dt.month
|
| 21 |
-
|
| 22 |
-
data['dob'] = pd.to_datetime(data['dob'])
|
| 23 |
-
data['age'] = (data['trans_date_trans_time'] - data['dob']).dt.days // 365
|
| 24 |
-
|
| 25 |
-
data['distance'] = np.sqrt(
|
| 26 |
-
(data['lat'] - data['merch_lat'])**2 +
|
| 27 |
-
(data['long'] - data['merch_long'])**2
|
| 28 |
-
) * 111
|
| 29 |
-
|
| 30 |
-
# Encodage
|
| 31 |
-
le_category = LabelEncoder()
|
| 32 |
-
le_gender = LabelEncoder()
|
| 33 |
-
le_state = LabelEncoder()
|
| 34 |
-
|
| 35 |
-
data['category_encoded'] = le_category.fit_transform(data['category'])
|
| 36 |
-
data['gender_encoded'] = le_gender.fit_transform(data['gender'])
|
| 37 |
-
data['state_encoded'] = le_state.fit_transform(data['state'])
|
| 38 |
-
|
| 39 |
-
# Statistiques par client
|
| 40 |
-
client_stats = data.groupby('cc_num').agg({
|
| 41 |
-
'amt': ['mean', 'std', 'count'],
|
| 42 |
-
'is_fraud': 'sum'
|
| 43 |
-
}).reset_index()
|
| 44 |
-
client_stats.columns = ['cc_num', 'avg_amt', 'std_amt', 'nb_trans', 'nb_fraud']
|
| 45 |
-
client_stats['std_amt'] = client_stats['std_amt'].fillna(0)
|
| 46 |
-
data = data.merge(client_stats, on='cc_num', how='left')
|
| 47 |
-
|
| 48 |
-
# Préparation des features
|
| 49 |
-
features = [
|
| 50 |
-
'amt', 'hour', 'day_of_week', 'day', 'month', 'age',
|
| 51 |
-
'category_encoded', 'gender_encoded', 'state_encoded',
|
| 52 |
-
'lat', 'long', 'city_pop', 'distance',
|
| 53 |
-
'avg_amt', 'std_amt', 'nb_trans'
|
| 54 |
-
]
|
| 55 |
-
|
| 56 |
-
X = data[features].fillna(0)
|
| 57 |
-
y = data['is_fraud']
|
| 58 |
-
|
| 59 |
-
# Split et entraînement
|
| 60 |
-
X_train, X_test, y_train, y_test = train_test_split(
|
| 61 |
-
X, y, test_size=0.3, random_state=42, stratify=y
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
print("Entraînement du Random Forest...")
|
| 65 |
-
model = RandomForestClassifier(
|
| 66 |
-
n_estimators=100,
|
| 67 |
-
max_depth=10,
|
| 68 |
-
random_state=42,
|
| 69 |
-
n_jobs=-1
|
| 70 |
-
)
|
| 71 |
-
model.fit(X_train, y_train)
|
| 72 |
-
|
| 73 |
-
# Évaluation
|
| 74 |
-
from sklearn.metrics import roc_auc_score, classification_report
|
| 75 |
-
y_pred_proba = model.predict_proba(X_test)[:, 1]
|
| 76 |
-
auc = roc_auc_score(y_test, y_pred_proba)
|
| 77 |
-
print(f"\nAUC-ROC: {auc:.4f}")
|
| 78 |
-
|
| 79 |
-
# Sauvegarde du modèle et des encodeurs
|
| 80 |
-
print("\nSauvegarde du modèle et des encodeurs...")
|
| 81 |
-
joblib.dump(model, 'fraud_model.pkl')
|
| 82 |
-
joblib.dump(le_category, 'le_category.pkl')
|
| 83 |
-
joblib.dump(le_gender, 'le_gender.pkl')
|
| 84 |
-
joblib.dump(le_state, 'le_state.pkl')
|
| 85 |
-
|
| 86 |
-
# Sauvegarde des mappings pour l'API
|
| 87 |
-
category_mapping = dict(zip(le_category.classes_, le_category.transform(le_category.classes_)))
|
| 88 |
-
gender_mapping = dict(zip(le_gender.classes_, le_gender.transform(le_gender.classes_)))
|
| 89 |
-
state_mapping = dict(zip(le_state.classes_, le_state.transform(le_state.classes_)))
|
| 90 |
-
|
| 91 |
-
mappings = {
|
| 92 |
-
'categories': category_mapping,
|
| 93 |
-
'genders': gender_mapping,
|
| 94 |
-
'states': state_mapping,
|
| 95 |
-
'features': features
|
| 96 |
-
}
|
| 97 |
-
|
| 98 |
-
def convert(o):
|
| 99 |
-
if isinstance(o, np.integer):
|
| 100 |
-
return int(o)
|
| 101 |
-
elif isinstance(o, np.floating):
|
| 102 |
-
return float(o)
|
| 103 |
-
elif isinstance(o, np.ndarray):
|
| 104 |
-
return o.tolist()
|
| 105 |
-
else:
|
| 106 |
-
return o
|
| 107 |
-
|
| 108 |
-
with open('mappings.json', 'w') as f:
|
| 109 |
-
json.dump(mappings, f, indent=2, default=convert)
|
| 110 |
-
|
| 111 |
-
print("\n✅ Modèle sauvegardé avec succès!")
|
| 112 |
-
print("Fichiers créés:")
|
| 113 |
-
print(" - fraud_model.pkl")
|
| 114 |
-
print(" - le_category.pkl, le_gender.pkl, le_state.pkl")
|
| 115 |
-
print(" - mappings.json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|