CoolFace
Apppublic

AIBotsForYou/Ensemble_Fraud_Detection

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
ensemble_model.py50 linesDownload Raw Back to models
1# models/ensemble_model.py
2
3import numpy as np
4import pandas as pd
5from sklearn.linear_model import LogisticRegression
6from sklearn.tree import DecisionTreeClassifier
7from sklearn.ensemble import RandomForestClassifier, VotingClassifier
8from sklearn.model_selection import train_test_split
9
10def load_data(csv_path: str):
11    """
12    Load the dataset from a CSV file.
13    Assumes the CSV has features and a target column named 'is_fraud'
14    """
15    df = pd.read_csv(csv_path)
16    X = df.drop("is_fraud", axis=1)
17    y = df["is_fraud"]
18    return X, y
19
20def train_ensemble(X, y):
21    """
22    Train an ensemble classifier using Logistic Regression, Decision Tree, and Random Forest.
23    Uses soft voting to support probability estimates required for ROC curve generation.
24    """
25    # Split data into training and testing sets
26    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
27
28    # Define individual classifiers
29    clf1 = LogisticRegression(max_iter=1000, solver='lbfgs')
30    clf2 = DecisionTreeClassifier(max_depth=5, random_state=42)
31    clf3 = RandomForestClassifier(n_estimators=100, random_state=42)
32
33    # Build the ensemble with soft voting to enable predict_proba
34    ensemble = VotingClassifier(estimators=[
35        ('lr', clf1), ('dt', clf2), ('rf', clf3)
36    ], voting='soft')
37
38    # Train the ensemble classifier
39    ensemble.fit(X_train, y_train)
40    
41    # Return the trained model and test data for evaluation
42    return ensemble, X_test, y_test
43
44if __name__ == "__main__":
45    # For quick testing: adjust the CSV path as needed.
46    csv_path = "../data/sample_transactions.csv"
47    X, y = load_data(csv_path)
48    model, X_test, y_test = train_ensemble(X, y)
49    print("Ensemble model trained successfully!")
50