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

标签 r statistics sample-size

我有一个包含大约 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/

相关文章:

r - Fisher.test R 崩溃,并出现 *** 捕获段错误 *** 错误

r - R 中的分层 Bootstrapping >25 层

r - r 中的样本大小和功率计算可以作为 SAS 中 proc power 的可行替代方案吗?

machine-learning - 执行随机森林时的最小观察次数

r - 试图删除 ggplot2 可视化上的边距

r - R中的QR分解和Cholesky分解

java - 如何在 java 中使用 apache math 3.0 为直方图生成 bins?

python - 使用 python 拟合经验分布

r - lme4 的混合模型起始值

r - 如何在单个密度图中获得多条线,并具有更正的比例?