随机过采样
只有极少一部分转账是欺诈。现在,您将对欺诈样本进行过采样,以平衡类别分布。数据集 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(___(___))