डेटासेट से डिटेक्शन मॉडल तक

R में Fraud Detection

Sebastiaan Höppner

PhD researcher in Data Science at KU Leuven

रोडमैप

  • (1) डेटासेट को training set और test set में बाँटें
  • (2) कोई machine learning model चुनें
  • (3) class distribution संतुलित करने के लिए training set पर SMOTE लागू करें
  • (4) re-balanced training set पर model train करें
  • (5) (original) test set पर performance test करें
R में Fraud Detection

Training और test सेट में विभाजन

  • डेटासेट को training set और test set में बाँटें (जैसे 50/50, 75/25, ...)
  • शुरुआत में, दोनों सेटों में class distribution एक जैसा रखें
  • उदाहरण: 50% training set और 50% test set
prop.table(table(train$Class))
   0    1 
0.98 0.02
prop.table(table(test$Class))
   0    1 
0.98 0.02
R में Fraud Detection

Machine learning model चुनें और train करें

  • 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 में Fraud Detection
library(partykit)
plot(as.party(model1))

पेड़1

R में Fraud Detection
## test set के लिए fraud probability प्रेडिक्ट करें
scores1 = predict(model1, newdata = test, type = "prob")[, 2]

## test set के लिए class (fraud या नहीं) प्रेडिक्ट करें 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 में Fraud Detection

Training set पर 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 में Fraud Detection
library(rpart)
model2 = rpart(Class ~ ., data = train_oversampled)

पेड़2

R में Fraud Detection
## test set के लिए fraud probability प्रेडिक्ट करें
scores2 = predict(model2, newdata = test, type = "prob")[, 2]

## test set के लिए class (fraud या नहीं) प्रेडिक्ट करें 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 में Fraud Detection

डिटेक्शन मॉडल लागू करने की लागत

  • एल्गोरिदम का मूल्यांकन करते समय fraud detection की अलग-अलग लागतें ध्यान में रखें
  • लागत जुड़ी होती है
    • misclassification errors (false positives और false negatives) से, और
    • सही वर्गीकरण (true positives और true negatives) से
R में Fraud Detection

Cost matrix

कॉस्ट_मैट्रिक्स_1

  • $y_i$ = केस $i$ का true class
  • $c_i$ = केस $i$ के लिए predicted class
R में Fraud Detection

Cost matrix

कॉस्ट_मैट्रिक्स_२

  • $y_i$ = केस $i$ का true class
  • $c_i$ = केस $i$ के लिए predicted class
R में Fraud Detection

Cost matrix

कॉस्ट_मैट्रिक्स_३

  • $C_a$ = केस की जाँच की लागत
R में Fraud Detection

Cost matrix

कॉस्ट_मैट्रिक्स_४

  • $C_a$ = केस की जाँच की लागत
R में Fraud Detection

डिटेक्शन मॉडल के लिए cost measure

  • हर केस की वास्तविक लागत जोड़ें: $$Cost(model)=\sum_{i=1}^{N}y_i(1-c_i)Amount_i + c_iC_a$$
    • $y_i$ = केस $i$ का true class
    • $c_i$ = केस $i$ के लिए predicted class
cost_model = function(predicted.classes, true.classes, amounts, fixedcost) {

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

    return(cost)
}
R में Fraud Detection

Fraud detection की वास्तविक लागत

## SMOTE के बिना कुल लागत:
cost_model(predicted_class1, test$Class, test$Amount, fixedcost = 10)
10061.8
## SMOTE के साथ कुल लागत:
cost_model(predicted_class2, test$Class, test$Amount, fixedcost = 10)
7431.93
  • नुकसान 26% घटे!
R में Fraud Detection

अभ्यास करते हैं!

R में Fraud Detection

Preparing Video For Download...