diff --git a/AI Credit Card scam detector b/AI Credit Card scam detector new file mode 100644 index 0000000..1afbce4 --- /dev/null +++ b/AI Credit Card scam detector @@ -0,0 +1,106 @@ +""" +AI Credit Card Scam Detector + +This script trains a machine learning model to detect credit card scams, +stores transactions and predictions in a local SQLite database, +and provides an interface to add new transactions for instant prediction. + +Author: Jlevy1106 +Date: 2025-08-04 +""" + +import sqlite3 +import pandas as pd +import numpy as np +from sklearn.ensemble import RandomForestClassifier +from sklearn.model_selection import train_test_split +from sklearn.metrics import classification_report +from sklearn.preprocessing import StandardScaler + +# ========================= +# === SAMPLE DATA BLOCK === +# ========================= +# You can replace this block with your own data or expand it. +data = { + 'amount': [100, 1500, 20, 5000, 77, 450, 3300, 15, 120, 2700], + 'location': [1, 0, 1, 0, 1, 1, 0, 1, 1, 0], # 1 = local, 0 = foreign + 'is_night': [0, 1, 0, 1, 0, 1, 1, 0, 0, 1], # 1 = night, 0 = day + 'card_age_months': [12, 2, 40, 1, 20, 6, 3, 60, 24, 4], + 'is_online': [1, 1, 0, 1, 0, 1, 1, 0, 0, 1], + 'scam': [0, 1, 0, 1, 0, 0, 1, 0, 0, 1] +} +df = pd.DataFrame(data) + +# =========================== +# === FEATURE EXTRACTION ==== +# =========================== +X = df.drop('scam', axis=1) +y = df['scam'] + +scaler = StandardScaler() +X_scaled = scaler.fit_transform(X) + +# =========================== +# === MODEL TRAINING BLOCK == +# =========================== +X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.3, random_state=42) +model = RandomForestClassifier(n_estimators=100, random_state=42) +model.fit(X_train, y_train) + +# =========================== +# === MODEL EVALUATION ====== +# =========================== +print("=== Model Evaluation ===") +predictions = model.predict(X_test) +print(classification_report(y_test, predictions)) + +# =========================== +# === SQLITE DATABASE SETUP = +# =========================== +conn = sqlite3.connect('credit_card_scam_ai.db') +c = conn.cursor() +c.execute(''' +CREATE TABLE IF NOT EXISTS transactions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + amount REAL, + location INTEGER, + is_night INTEGER, + card_age_months INTEGER, + is_online INTEGER, + predicted_scam INTEGER +) +''') +conn.commit() + +def add_transaction(amount, location, is_night, card_age_months, is_online): + """ + Adds a transaction to the database and predicts if it is a scam. + Parameters: + amount (float): Transaction amount + location (int): 1 for local, 0 for foreign + is_night (int): 1 for night, 0 for day + card_age_months (int): Age of card in months + is_online (int): 1 for online, 0 for offline + """ + features = np.array([[amount, location, is_night, card_age_months, is_online]]) + features_scaled = scaler.transform(features) + predicted_scam = int(model.predict(features_scaled)[0]) + c.execute(''' + INSERT INTO transactions (amount, location, is_night, card_age_months, is_online, predicted_scam) + VALUES (?, ?, ?, ?, ?, ?) + ''', (amount, location, is_night, card_age_months, is_online, predicted_scam)) + conn.commit() + print(f"Transaction added. Scam prediction: {'SCAM' if predicted_scam else 'NOT SCAM'}") + +# =========================== +# === SAMPLE USAGE ========== +# =========================== +if __name__ == "__main__": + print("\n=== Adding Example Transaction ===") + add_transaction(2000, 0, 1, 2, 1) # Foreign, night, new card, online + + print("\n=== All Transactions in Database ===") + for row in c.execute('SELECT * FROM transactions'): + print(row) + + conn.close()