ML Project – Credit Card Fraud Detection using Random Forest

Machine Learning courses with 100+ Real-time projects Start Now!!

Program 1

Credit Card Fraud Dataset

import pandas as pd
import numpy as np
from tkinter import *
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns

# Load dataset
df = pd.read_csv("D://scikit_data/card/credit_card_fraud_large.csv")

df.head()

df.isnull().sum()

X = df.drop("IsFraud", axis=1) # Independed variables
y = df["IsFraud"] # Depended variables

X

y

# Train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X, y)

# GUI setup
root = Tk()
root.title("Credit Card Fraud Predictor")
root.geometry("400x400")

# Input fields
Label(root, text="Transaction Amount",font=("Arial", 10, "bold") ).pack()
amt_entry = Entry(root)
amt_entry.pack()

Label(root, text="Transaction Time",font=("Arial", 10, "bold")).pack()
time_entry = Entry(root)
time_entry.pack()

Label(root, text="Location Risk (0=Safe, 1=Risky)",font=("Arial", 10, "bold")).pack()
loc_entry = Entry(root)
loc_entry.pack()

Label(root, text="Card Type (0=Debit, 1=Credit)",font=("Arial", 10, "bold")).pack()
card_entry = Entry(root)
card_entry.pack()

result_label = Label(root, text="", font=("Arial", 12, "bold"))
result_label.pack(pady=10)

def predict_fraud():
    amt = float(amt_entry.get())
    time = float(time_entry.get())
    loc = int(loc_entry.get())
    card = int(card_entry.get())

    pred = model.predict([[amt, time, loc, card]])
    result = " Fraudulent Transaction!" if pred[0] == 1 else " Transaction Safe."
    result_label.config(text=result)

def show_feature_importance():
    importance = model.feature_importances_
    features = X.columns
    sns.barplot(x=importance, y=features, palette="Set2")
    plt.title("Feature Importance in Fraud Detection")
    plt.xlabel("Importance Score")
    plt.ylabel("Feature")
    plt.tight_layout()
    plt.show()

Button(root, text="Predict", command=predict_fraud, bg="green", fg="white").pack(pady=10)
Button(root, text="Show Feature Importance", command=show_feature_importance, bg="blue", fg="white").pack()

root.mainloop()

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *