-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
52 lines (39 loc) · 1.71 KB
/
train_model.py
File metadata and controls
52 lines (39 loc) · 1.71 KB
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
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score
from imblearn.over_sampling import SMOTE
import pickle
print("Starting Fraud Detection Model Training...")
print("Loading dataset...")
data = pd.read_csv(r"C:\Users\HP\Downloads astha\Fraud_Detection_Project\data\creditcard.csv")
print("Scaling Amount and Time...")
scaler = StandardScaler()
data['Amount'] = scaler.fit_transform(data['Amount'].values.reshape(-1, 1))
data['Time'] = scaler.fit_transform(data['Time'].values.reshape(-1, 1))
print("Preparing features and target...")
X = data.drop('Class', axis=1)
y = data['Class']
print("Splitting train and test data...")
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
print("Applying SMOTE to handle imbalance...")
sm = SMOTE(random_state=42)
X_train_res, y_train_res = sm.fit_resample(X_train, y_train)
print("Training Random Forest model...")
rf = RandomForestClassifier(n_estimators=100, random_state=42)
rf.fit(X_train_res, y_train_res)
print("Saving model...")
with open(r"C:\Users\HP\Downloads astha\Fraud_Detection_Project\models\rf_model.pkl", "wb") as f:
pickle.dump(rf, f)
print("Evaluating model...")
y_pred = rf.predict(X_test)
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))
print("ROC-AUC Score:", roc_auc_score(y_test, y_pred))
print("\n✅ Training completed successfully!")