Ashya-Joselin/Credit-Card-Fraud-Detection-System
GitHub: Ashya-Joselin/Credit-Card-Fraud-Detection-System
基于机器学习的信用卡欺诈检测系统,结合 FastAPI 实时预测与规则风控评分,提供端到端的模型训练与部署方案。
Stars: 0 | Forks: 0
# 信用卡欺诈检测系统
这是一个基于 Machine Learning 的端到端信用卡欺诈检测系统,它使用训练好的分类模型来预测欺诈交易。该项目包括模型训练、评估、用于实时预测的 FastAPI 后端、Docker 支持以及基于规则的风险评估。
## 功能
- 用于欺诈检测的 Machine Learning 模型
- 用于实时预测的 FastAPI REST API
- 基于规则的交易风险评估
- 模型训练、超参数调优和评估脚本
- Docker 化的应用程序,便于部署
- 模块化的项目结构
- 使用 Pydantic 进行输入验证
## 项目结构
```
Credit-Card-Fraud-Detection/
│
├── app/
│ ├── main.py # FastAPI application
│ ├── model_loader.py # Loads trained model
│ └── schemas.py # Request/Response schemas
│
├── model/
│ ├── fraud_model.pkl # Trained ML model
│ ├── risk_logic.py # Rule-based risk scoring
│ └── __init__.py
│
├── training/
│ ├── train.py # Train model
│ ├── evaluate.py # Model evaluation
│ ├── tune.py # Hyperparameter tuning
│ └── __init__.py
│
├── data/
│ └── creditcard.csv # Dataset
│
├── Dockerfile
├── requirements.txt
├── README.md
└── .gitignore
```
## 技术栈
- Python 3.10
- FastAPI
- Scikit-learn
- Pandas
- NumPy
- Joblib/Pickle
- Uvicorn
- Docker
## 数据集
该项目使用 **Credit Card Fraud Detection Dataset**,其中包含匿名的交易特征(`V1`–`V28`)以及交易 `Amount` 和 `Time`。
目标变量:
- **0** → 正常交易
- **1** → 欺诈交易
## 安装
克隆仓库
```
git clone https://github.com/Ashya-Joselin/Credit-Card-Fraud-Detection-System.git
```
进入项目目录
```
cd Credit-Card-Fraud-Detection
```
安装依赖
```
pip install -r requirements.txt
```
## 运行 API
启动 FastAPI 服务器
```
uvicorn app.main:app --reload
```
服务器运行于
```
http://127.0.0.1:8000
```
交互式 API 文档
```
http://127.0.0.1:8000/docs
```
替代文档
```
http://127.0.0.1:8000/redoc
```
## 训练模型
训练模型
```
python training/train.py
```
评估模型
```
python training/evaluate.py
```
超参数调优
```
python training/tune.py
```
## Docker
构建 Docker 镜像
```
docker build -t fraud-detection .
```
运行容器
```
docker run -p 8000:8000 fraud-detection
```
## API Endpoint
### POST `/predict`
预测交易是否为欺诈。
### 请求示例
```
{
"Time": 1000,
"Amount": 250.50,
"V1": 0.12,
"V2": -0.45,
"...": "...",
"V28": 0.08
}
```
### 响应示例
```
{
"prediction": "Fraud",
"risk_score": 0.96
}
```
## 模型工作流
1. 加载交易数据
2. 预处理特征
3. 训练分类模型
4. 保存训练好的模型
5. 使用 FastAPI 加载模型
6. 预测欺诈概率
7. 应用基于规则的风险逻辑
8. 通过 REST API 返回预测结果
## 未来改进
- 支持 XGBoost 和 LightGBM
- 使用 SHAP 实现可解释 AI
- 模型监控
- 数据库集成
- JWT 认证
- 使用 GitHub Actions 实现 CI/CD
- 云部署 (AWS/Azure/GCP)
## 许可证
本项目仅用于教育和个人作品展示目的。
## 作者
**Ashya J**
标签:Apex, AV绕过, Docker, FastAPI, 安全防御评估, 机器学习, 模型训练, 欺诈检测, 请求拦截, 逆向工具, 金融科技