隨機過度抽樣(Random over-sampling)
只有極少部分的信用卡轉帳是詐欺。你現在要對詐欺類別進行過度抽樣(over-sample),以平衡類別分佈。資料集 creditcard 中的特徵 Class,在詐欺時取值為 1,否則為 0。
你可以在主控台用 str() 檢視 creditcard 的欄位,用 head() 列出前 6 筆資料,並用 table(creditcard$Class) 檢查類別平衡。
本練習屬於課程
R 的詐欺偵測
練習說明
- 載入
ROSE套件。 - 指定過度抽樣後資料集所需的個案數
n_new,使新資料集中詐欺佔 30%、合法佔 70%。為此,請把現有的合法個案數除以過度抽樣後欲達成的合法比例。 - 使用
ovun.sample()進行過度抽樣,公式為Class ~ .。 - 檢查過度抽樣後資料集的類別平衡。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Load ROSE
___
# Calculate the total number of required cases in the over-sampled dataset
print(table(creditcard$Class))
n_new <- ___
# Over-sample
oversampling_result <- ___(formula = ___, data = ___,
method = ___, N = ___, seed = 2018)
# Verify the Class-balance of the over-sampled dataset
oversampled_credit <- oversampling_result$data
prop.table(___(___))