不正検知の真のコスト
元の訓練データで学習したモデル(model_orig)と、再サンプリング後の訓練データで学習したモデル(model_smote)の2つを用意しました。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()を使って、fixedcostを 10 としてmodel_origをテストセットに適用したときの真のコストを計算してください。cost_model()を使って、fixedcostを 10 としてmodel_smoteをテストセットに適用したときの真のコストを計算してください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Calculate the total cost of deploying the original model
cost_model(___, ___, ___, ___)
# Calculate the total cost of deploying the model using SMOTE
cost_model(___, ___, ___, ___)