縮減多數群組
與其增加資料集中的詐欺案例數量,你也可以隨機「移除合法案例來平衡資料集」。讓我們在 creditcard 資料集中,對多數類別(Class = 0)執行欠抽樣。你可以在主控台使用 table() 查看資料集中有多少詐欺與合法交易。
本練習屬於課程
R 的詐欺偵測
練習說明
- 載入 ROSE 函式庫。
- 將
n_new指定為欠抽樣後資料集所需的案例數,使新資料集中詐欺案例佔 40%。為此,請用詐欺案例數除以欠抽樣後期望的詐欺比例。 - 對資料集進行欠抽樣。
- 使用
table()和prop.table()檢查欠抽樣後資料集的類別平衡。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Load ROSE
___
# Calculate the required number of cases in the over-sampled dataset
n_new <- ___
# Under-sample
undersampling_result <- ___(formula = ___, data = ___,
___ = ___, ___ = ___, seed = 2018)
# Verify the Class-balance of the under-sampled dataset
undersampled_credit <- undersampling_result$___
___(___(___))