詐欺偵測的真實成本
你已經在原始訓練集(model_orig)與重新取樣後的訓練集(model_smote)各建立了一個模型。對於 test 測試集中各筆案例的預測類別,分別存於 predicted_class_orig 與 predicted_class_smote。與其用準確率來比較詐欺偵測模型,更好的方式是計算其偵測成本。
以下是 cost_model() 函式的定義。請先閱讀,以理解成本如何計算。
cost_model <- function(predicted.classes, true.classes, amounts, fixedcost) {
library(hmeasure)
predicted.classes <- relabel(predicted.classes)
true.classes <- relabel(true.classes)
cost <- sum(true.classes * (1 - predicted.classes) * amounts + predicted.classes * fixedcost)
return(cost)
}
本練習屬於課程
R 的詐欺偵測
練習說明
- 使用
cost_model()計算在測試集部署model_orig的真實成本,fixedcost設為 10。 - 使用
cost_model()計算在測試集部署model_smote的真實成本,fixedcost設為 10。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Calculate the total cost of deploying the original model
cost_model(___, ___, ___, ___)
# Calculate the total cost of deploying the model using SMOTE
cost_model(___, ___, ___, ___)