R语言DMwR包中的SMOTE函数中perc.over和perc.under的含义。

通过示例展示如何使用SMOTE算法解决不平衡数据集问题,包括数据准备、参数设置及结果验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引文:帮助文档http://www.biostatistic.net/thread-34529-1-1.html
同样可以载入DMwR包之后,输入 ?SMOTE 查看相关帮助文档

perc.over = xx 表示 少样本变成原来的(1+xx/100)倍
perc.under=yy 表示多样本变成少样本的 yy/100 *(xx/100)倍

## 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 set[#检查类的分布,这种人工数据集]
table(data$Species)

## now using SMOTE to create a more "balanced problem"[#现在使用SMOTE的创建一个更加“平衡的问题”]
newData <- SMOTE(Species ~ ., data, perc.over = 600,perc.under=100)
table(newData$Species)
> table(data$Species)

common   rare 
   100     50 
> 
> ## now using SMOTE to create a more "balanced problem"[#现在使用SMOTE的创建一个更加“平衡的问题”]
> newData <- SMOTE(Species ~ ., data, perc.over = 600,perc.under=100)
> table(newData$Species)

common   rare 
   300    350 

本文中,原始数据 多样本:少样本为 100:50
设置perc.over = 600,perc.under=100
即 少样本 变成原来的 1+600/100=7倍,计50*7=350个
多样本变为 少样本的 100/100*(600/100)=6倍,计50*6=300个

自己试试 吧

> install.packages("https://cran.r-project.org/src/contrib/Archive/DMwR/DMwR_0.4.1.tar.gz", repos=NULL, type="source") 试开URL’https://cran.r-project.org/src/contrib/Archive/DMwR/DMwR_0.4.1.tar.gz' Content type 'application/x-gzip' length 1941361 bytes (1.9 MB) downloaded 1.9 MB * installing *source* package 'DMwR' ... ** package 'DMwR' successfully unpacked and MD5 sums checked ** using staged installation ** R ** data *** moving datasets to lazyload DB ** inst ** byte-compile and prepare package for lazy loading ** help *** installing help indices ** building package indices ** testing if installed package can be loaded from temporary location ** testing if installed package can be loaded from final location ** testing if installed package keeps a record of temporary installation path * DONE (DMwR) > library(DMwR2) > # === 用户自定义区域 ============================================ > # 1. 数据加载 > dat = read.csv("C:/Users/53145/onedrive/Desktop/lasso_selected.csv", fileEncoding = "UTF-8") > test_data <- read.csv("C:/Users/53145/OneDrive/Desktop/external_DL.csv", fileEncoding = "UTF-8") > head(dat) class DL_1 DL_2 NWU ASPECTS mTICI 1 DN -12.37837900 5.7445570 7.07 4 1 2 DN -25.83022700 -3.6276097 6.64 5 0 3 Control 5.18351800 -8.5702720 7.98 8 1 4 Control 5.71541700 0.6470367 7.39 4 1 5 DN 0.07879532 7.8264100 6.16 8 1 6 DN -15.08929100 2.4736260 9.84 7 1 > # 使用 createDataPartition 进行分层抽样,按 7:3 划分数据 > trainIndex <- createDataPartition(dat$class, p = 0.7, list = FALSE, times = 1) > # 根据生成的索引划分数据 > train_data <- dat[trainIndex, ] > val_data <- dat[-trainIndex, ] > # 输出训练集验证集的类别分布 > print(table(train_data$class)) Control DN 70 35 > print(table(val_data$class)) Control DN 30 15 > # === 使用 SMOTE 进行过采样 ======================================= > # 使用 SMOTE 进行过采样,'class ~ .' 表示基于训练数据中的所有特征进行过采样 > train_data_smote <- DMwR2::SMOTE(class ~NWU, data = train_data, perc.over = 200, perc.under = 100) 错误: 'SMOTE' is not an exported object from 'namespace:DMwR2'
最新发布
07-20
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值