缩减多数类
与其增加数据集中的欺诈样本数量,您也可以随机移除合法样本来平衡数据集。现在请对 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$___
___(___(___))