利用R语言的DMwR包处理样本不平衡

来源:互联网 发布:网络的好处英语作文 编辑:程序博客网 时间:2024/05/24 01:47
在R的DMwR包中提供了SMOTE函数用于不平衡的分类问题,其背后的原理是SMOTE算法。SMOTE函数产生一个新的数据集来解决分类不平衡的问题。用法: SMOTE(form, data, perc.over = 200, k = 5, perc.under = 200, learner = NULL, ...) 参数解释: form:描述预测问题的公式 data:原始的不平衡的数据集 k:用于产生新的少数派样本的最近邻数量(与kNN中的k类似) learner:可选参数,指定一个分类算法的函数名称(类型为字符串),这个函数将对结果进行分类 **perc.over:指定从少数样本中采样的比例。** **perc.under:对多数样本下采样,有多少比例的样本被选入新的数据集中**关于perc.over和perc.under的具体解释:假设初始数据集中有N个少数样本和M个多数的样本,perc.over=a,perc.under=b。首先增加少数派样本的数量,平均每个样本增加a/100个新样本,一共新增了aN/100个全新的少数派样本,并把最初的少数派样本和新增的少数派样本都放入新的数据集中。然后对多数派的样本进行采样,采样数量为(b/100) * aN/100,得到新的多数派样本,将新的多数派样本放入到新的数据集中,这样新的数据集中,少数派样本有(1+a/100)N个,多数派样本有(b/100) * aN/100个perc.over 不能为0

在R的help文档中符了一个案例:

## A small example with a data set created artificially from the IRIS## data data(iris)data <- iris[, c(1, 2, 5)]data$Species <- factor(ifelse(data$Species == "setosa","rare","common")) ## checking the class distribution of this artificial data settable(data$Species)## now using SMOTE to create a more "balanced problem"newData <- SMOTE(Species ~ ., data, perc.over = 600,perc.under=100)table(newData$Species)## Checking visually the created data## Not run: par(mfrow = c(1, 2))plot(data[, 1], data[, 2], pch = 19 + as.integer(data[, 3]),     main = "Original Data")plot(newData[, 1], newData[, 2], pch = 19 + as.integer(newData[,3]),     main = "SMOTE'd Data")## End(Not run)## Now an example where we obtain a model with the "balanced" dataclassTree <- SMOTE(Species ~ ., data, perc.over = 600,perc.under=100,                   learner='rpartXse',se=0.5)## check the resulting classification treeclassTree## The tree with the unbalanced data set would berpartXse(Species ~ .,data,se=0.5)
0 0
原创粉丝点击