จากชุดข้อมูลสู่โมเดลตรวจจับ

การตรวจจับการฉ้อโกงใน R

Sebastiaan Höppner

PhD researcher in Data Science at KU Leuven

แผนงาน

  • (1) แบ่งชุดข้อมูลเป็น ชุดฝึก และ ชุดทดสอบ
  • (2) เลือกโมเดล machine learning
  • (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

เลือกและฝึกโมเดล machine learning

  • Decision tree, artificial neural network, support vector machines, logistic regression, random forest, Naive Bayes, k-Nearest Neighbors, ...
  • ตัวอย่าง: อัลกอริทึม Classification And Regression Tree (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

ต้นทุนของการนำโมเดลตรวจจับไปใช้

  • คำนึงถึง ต้นทุนของการตรวจจับการฉ้อโกง ในการประเมินอัลกอริทึม
  • ต้นทุนเกี่ยวข้องกับ
    • ข้อผิดพลาดในการจำแนก (false positives และ false negatives) และ
    • การจำแนกที่ถูกต้อง (true positives และ true negatives)
การตรวจจับการฉ้อโกงใน R

Cost matrix

cost_matrix_1

  • $y_i$ = คลาสที่แท้จริงของกรณี $i$
  • $c_i$ = คลาสที่พยากรณ์สำหรับกรณี $i$
การตรวจจับการฉ้อโกงใน R

Cost matrix

cost_matrix_2

  • $y_i$ = คลาสที่แท้จริงของกรณี $i$
  • $c_i$ = คลาสที่พยากรณ์สำหรับกรณี $i$
การตรวจจับการฉ้อโกงใน R

Cost matrix

cost_matrix_3

  • $C_a$ = ต้นทุนในการวิเคราะห์กรณี
การตรวจจับการฉ้อโกงใน R

Cost matrix

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

มาฝึกกันเถอะ!

การตรวจจับการฉ้อโกงใน R

Preparing Video For Download...