kumarkislay245-spec/fraud-detection-system

GitHub: kumarkislay245-spec/fraud-detection-system

一个基于XGBoost的实时金融欺诈检测引擎,通过自定义特征工程和针对极度不平衡数据的特殊调优,在630万条交易中精准识别欺诈行为。

Stars: 1 | Forks: 0


![Python](https://img.shields.io/badge/Python-3776AB?style=for-the-badge&logo=python&logoColor=white) ![XGBoost](https://img.shields.io/badge/XGBoost-189AB4?style=for-the-badge) ![Streamlit](https://img.shields.io/badge/Streamlit-FF4B4B?style=for-the-badge&logo=streamlit&logoColor=white) ![Plotly](https://img.shields.io/badge/Plotly-3F4F75?style=for-the-badge&logo=plotly&logoColor=white) ![Pandas](https://img.shields.io/badge/Pandas-150458?style=for-the-badge&logo=pandas&logoColor=white) [![GitHub 仓库](https://img.shields.io/badge/View%20Repository-181717?style=for-the-badge&logo=github&logoColor=white)](https://github.com/kumarkislay245-spec/fraud-detection-system) [![作者](https://img.shields.io/badge/Author-Kislay%20Kumar-50c88c?style=for-the-badge)](https://github.com/kumarkislay245-spec)
## 🏦 这是什么? 一个**企业级欺诈检测引擎**,可以实时分析金融交易并对欺诈风险进行评分。专门为处理最棘手的机器学习问题——极端的类别不平衡而构建——在 630 万笔交易中,只有 **0.13%** 实际上是欺诈交易。 该系统结合了经过训练的 **XGBoost 分类器**、自定义特征工程以及一个实时 **Streamlit 仪表板**,任何人都可以输入一笔交易,并立即看到风险评估结果和相关的可视化图表。 ## 📊 核心挑战 —— 不平衡分类 ``` Total transactions: 6,300,000 Fraudulent cases: 8,213 (≈ 0.13%) Legitimate cases: 6,291,787 (≈ 99.87%) ``` 如果使用一个简单的模型,对每笔交易都预测为“非欺诈”,可以得到 **99.87% 的准确率** —— 但却漏掉了所有的欺诈交易。这正是本项目专门设计以避免的陷阱。 **解决方案:** 调整 XGBoost 的 `scale_pos_weight` 参数,使得对漏报欺诈行为的惩罚远远大于对误报的惩罚 —— 因为在金融领域,漏掉一次欺诈所造成的损失远比拦截一笔正常交易大得多。 ## ✨ 功能特性 | 特性 | 详情 | |---|---| | 🧠 **XGBoost 风险引擎** | 基于包含 630 万条记录的数据集进行训练,并采用了针对不平衡数据的配置 | | ⚙️ **自定义特征工程** | `errorBalanceOrg` 和 `errorBalanceDest` 揭示了隐藏的余额差异 | | 🛡️ **前置机器学习业务逻辑** | 在调用模型之前,自动拒绝 `amount > sender balance`(金额大于发送方余额)的交易 | | 📊 **交互式仪表板** | 分屏式 Streamlit UI —— 左侧为输入区,右侧为实时风险评估 | | 📈 **Plotly 可视化** | 使用柱状图对比资金可用性和余额误差指标 | | 🔢 **概率评分** | 连续的欺诈概率(0–100%),而不仅仅是简单的二元(是/否)标记 | | ⚡ **模型缓存加载** | `@st.cache_resource` 确保模型只加载一次并保留在内存中 | ## 🧠 工作原理 ``` User enters transaction details (amount, sender balances, receiver balances) │ ▼ Business Logic Check: amount > sender balance? YES → Reject immediately (Insufficient Funds) NO → Continue to ML model │ ▼ Feature Engineering: errorBalanceOrg = newBalanceOrig + amount - oldBalanceOrg errorBalanceDest = oldBalanceDest + amount - newBalanceDest │ ▼ XGBoost model predicts fraud probability │ ▼ prob > 0.10 → 🚨 TRANSACTION BLOCKED prob ≤ 0.10 → ✅ TRANSACTION APPROVED │ ▼ Display Plotly charts + technical metrics ``` ### 为什么需要 `errorBalance` 特征? 原始的交易金额并不能说明全部问题。欺诈者可能转出了 10,000 卢比,但发送方的余额却没有减少 10,000 卢比 —— 这里存在差异。`errorBalance` 特征正是用来捕捉这种原始金额完全忽略的不匹配情况。 ``` # 关键 feature engineering 步骤 errorBalanceOrg = newBalanceOrig + amount - oldBalanceOrg # should be ~0 if legit errorBalanceDest = oldBalanceDest + amount - newBalanceDest # should be ~0 if legit ``` 这里的任何非零值都表明资金的实际流向与交易声明的流向不符。 ## 📁 项目结构 ``` fraud-detection-system/ │ ├── file.py # Streamlit app (full dashboard + ML inference) ├── model.json # Trained XGBoost model (JSON format) ├── requirements.txt # Python dependencies ├── .gitignore └── README.md ``` ## 🚀 本地运行 ### 1. 克隆仓库 ``` git clone https://github.com/kumarkislay245-spec/fraud-detection-system.git cd fraud-detection-system ``` ### 2. 安装依赖 ``` pip install -r requirements.txt ``` ### 3. 启动应用 https://fraud-detection-system-tvmo2rt687twsxnf2lnm4m.streamlit.app/ ## 📦 依赖项 ``` streamlit xgboost pandas plotly ``` 安装所有依赖: ``` pip install -r requirements.txt ``` ## 🖥️ 仪表板预览 **示例 —— 正常交易:** - 金额:₹1,000 | 发送方余额:₹5,000 → ₹4,000 | 接收方余额:₹0 → ₹1,000 - 结果:✅ **已批准** —— 余额对账无误,误差评分低 **示例 —— 欺诈模式:** - 金额:₹50,000 | 发送方余额:₹50,000 → ₹0 | 接收方余额:₹0 → ₹0(资金凭空消失) - 结果:🚨 **已拦截** —— `errorBalanceDest` 激增,模型给出高欺诈概率评分 ## 🔑 关键技术决策 **为什么选择 XGBoost 而不是 Logistic Regression 或 Random Forest?** XGBoost 在处理表格型金融数据方面表现极其出色,并且原生支持用于处理不平衡类别的 `scale_pos_weight`。与深度学习方法相比,它在大型数据集上的训练速度也更快。 **为什么阈值为 0.10 而不是 0.50?** 在 0.50 的阈值下,由于先验概率仅为 0.13%,模型会漏掉大部分欺诈行为。将阈值降低到 0.10 可以显著提高 Recall(查全率,即捕获真实欺诈的能力),代价仅仅是增加少量的 False Positive(误报)—— 这在金融欺诈检测中是一种可接受的权衡。 **为什么选择 JSON 模型格式而不是 pickle?** `model.json`(XGBoost 的原生格式)比 Python pickle 更安全、版本更稳定且更具可移植性 —— 它可以在任何版本的 XGBoost 中加载,而没有兼容性问题。 ## 👨‍💻 作者 **Kislay Kumar** —— NIT Warangal [![GitHub](https://img.shields.io/badge/GitHub-kumarkislay245--spec-181717?style=flat-square&logo=github)](https://github.com/kumarkislay245-spec)
标签:Apex, Kubernetes, Streamlit, XGBoost, 机器学习, 欺诈检测, 访问控制, 逆向工具, 金融风控