从数据集到检测模型

R 中的欺诈检测

Sebastiaan Höppner

PhD researcher in Data Science at KU Leuven

路线图

  • (1) 将数据集分为训练集和测试集
  • (2) 选择一个机器学习模型
  • (3) 在训练集上应用 SMOTE以平衡类别分布
  • (4) 在再平衡的训练集上训练模型
  • (5) 在(原始)测试集上评估性能
R 中的欺诈检测

将数据集划分为训练集与测试集

  • 将数据集拆分为训练集与测试集(如 50/50、75/25 等)
  • 确保两者初始类别分布一致
  • 例:50% 训练集,50% 测试集
prop.table(table(train$Class))
   0    1 
0.98 0.02
prop.table(table(test$Class))
   0    1 
0.98 0.02
R 中的欺诈检测

选择并训练机器学习模型

  • 决策树、人工神经网络、支持向量机、逻辑回归、随机森林、朴素贝叶斯、k 近邻等
  • 示例: 分类与回归树(CART)算法
  • rpart 包中的 rpart 函数
library(rpart)

model1 = rpart(Class ~ ., data = train)
R 中的欺诈检测
library(partykit)
plot(as.party(model1))

tree1

R 中的欺诈检测
## Predict fraud probability of test set
scores1 = predict(model1, newdata = test, type = "prob")[, 2]

## Predict class (fraud or not) of test set predicted_class1 = factor(ifelse(scores1 > 0.5, 1, 0))
## Confusion matrix & accuracy, library(caret) CM1 = confusionMatrix(data = predicted_class1, reference = test$Class)
          Reference         
Prediction     0     1
         0 12046    55
         1     8   191       Accuracy : 0.994878
library(pROC)
auc(roc(response = test$Class, predictor = scores1)) ## Area Under ROC Curve (AUC)
Area under the ROC curve: 0.8938
R 中的欺诈检测

在训练集上应用 SMOTE

library(smotefamily)
set.seed(123)

smote_result = SMOTE(X = train[, -17],
                     target = train$Class,
                     K = 5,
                     dup_size = 10)

train_oversampled = smote_result$data colnames(train_oversampled)[17] = "Class"
prop.table(table(train_oversampled$Class))
        0         1 
0.8166667 0.1833333
R 中的欺诈检测
library(rpart)
model2 = rpart(Class ~ ., data = train_oversampled)

tree2

R 中的欺诈检测
## Predict fraud probability of test set
scores2 = predict(model2, newdata = test, type = "prob")[, 2]

## Predict class (fraud or not) of test set predicted_class2 = factor(ifelse(scores2 > 0.5, 1, 0))
## Confusion matrix & accuracy library(caret) CM2 = confusionMatrix(data = predicted_class2, reference = test$Class)
          Reference
Prediction     0     1
         0 11967    34
         1    87   212       Accuracy : 0.9901626                                
library(pROC)
auc(roc(response = test$Class, predictor = scores2)) ## Area Under ROC Curve (AUC)
Area under the curve: 0.9538
R 中的欺诈检测

部署检测模型的成本

  • 在评估算法时考虑不同的欺诈检测成本
  • 成本来自于:
    • 误分类错误(假阳性与假阴性),以及
    • 正确分类(真阳性与真阴性)
R 中的欺诈检测

成本矩阵

cost_matrix_1

  • $y_i$ = 第 $i$ 个样本的真实类别
  • $c_i$ = 第 $i$ 个样本的预测类别
R 中的欺诈检测

成本矩阵

cost_matrix_2

  • $y_i$ = 第 $i$ 个样本的真实类别
  • $c_i$ = 第 $i$ 个样本的预测类别
R 中的欺诈检测

成本矩阵

cost_matrix_3

  • $C_a$ = 案例分析成本
R 中的欺诈检测

成本矩阵

cost_matrix_4

  • $C_a$ = 案例分析成本
R 中的欺诈检测

检测模型的成本度量

  • 考虑每个样本的实际成本: $$Cost(model)=\sum_{i=1}^{N}y_i(1-c_i)Amount_i + c_iC_a$$
    • $y_i$ = 第 $i$ 个样本的真实类别
    • $c_i$ = 第 $i$ 个样本的预测类别
cost_model = function(predicted.classes, true.classes, amounts, fixedcost) {

    cost = sum(true.classes * (1 - predicted.classes) * amounts +
               predicted.classes * fixedcost)

    return(cost)
}
R 中的欺诈检测

欺诈检测的真实成本

## Total cost without using SMOTE:
cost_model(predicted_class1, test$Class, test$Amount, fixedcost = 10)
10061.8
## Total cost when using SMOTE:
cost_model(predicted_class2, test$Class, test$Amount, fixedcost = 10)
7431.93
  • 损失降低了 26%!
R 中的欺诈检测

Let's practice!

R 中的欺诈检测

Preparing Video For Download...