gpt4 book ai didi

r - r 中的 SMOTE 显着减少了样本量

转载 作者:行者123 更新时间:2023-12-05 03:02:39 26 4
gpt4 key购买 nike

我有一个包含大约 130000 条记录的数据集。记录分为两类目标变量,0和1。1只占总比例的0.09%。

我在 Windows 10 上的 R-3.5.1 中运行我的分析。我使用 SMOTE 算法来处理这个不平衡的数据集。

我使用下面的代码来处理不平衡的数据集

library(DMwR)
data_code$target=as.factor(data_code$target) #Converted to factor as
# SMOTE works with factor data type
smoted_data <- SMOTE(target~., data_code, perc.over=100)

但是在执行代码之后,我看到 0 的计数是 212,1 也是 212,这大大减少了我的样本量。你能建议我如何在不改变我的情况下使用 SMOTE 处理这个不平衡的数据集吗数据大小

最佳答案

您需要尝试使用函数中的两个可用参数:perc.overperc.under

根据 doc来自 SMOTE:

The parameters perc.over and perc.under control the amount of over-sampling of the minority class and under-sampling of the majority classes, respectively.

所以:

perc.over will tipically be a number above 100. With this type of values, for each case in the orginal data set belonging to the minority class, perc.over/100 new examples of that class will be created

我看不到你的数据,但是,如果你的少数类有 100 个案例并且 perc.over=100,该算法将从该类中生成 100/100 = 1 个新案例。

The parameter perc.under controls the proportion of cases of the majority class that will be randomly selected for the final "balanced" data set. This proportion is calculated with respect to the number of newly generated minority class cases.

因此,例如 perc.under=100 的值将从原始数据的多数类中选择与为少数类生成的相同数量的观察值。

在我们的示例中,只生成了 1 个新案例,因此它只会添加另一个案例,从而产生一个包含 2 个案例的新数据集。

我建议为 perc.over 使用 100 以上的值,为 perc.under 使用更高的值(默认值为 100 和 200)。

请记住,您要在少数类中添加不真实的新观察结果,我会尽量控制这些。

数值示例:

set.seed(123)

data <- data.frame(var1 = sample(50),
var2 = sample(50),
out = as.factor(rbinom(50, 1, prob=0.1)))

table(data$out)
# 0 1
# 43 7 # 50 rows total (original data)
smote_data <- DMwR::SMOTE(out ~ var1, data, perc.over = 200, perc.under = 400)
table(smote_data$out)
# 0 1
# 56 21 # 77 rows total (smote data)

关于r - r 中的 SMOTE 显着减少了样本量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54625093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com