我试图使用SMOTE对数据集进行超采样,但我一直遇到此错误。
trainSM <- SMOTE(conversion ~ ., train,perc.over = 1000,perc.under = 200)
Run Code Online (Sandbox Code Playgroud)
矩阵中的错误(unlist(值,递归= FALSE,use.names = FALSE),nrow = nr ,:'dimnames'[2]的长度不等于数组范围
我的数据集如下:
conversion horizon length_of_stay guests rooms price comp_price
(dbl) (int) (int) (int) (int) (int) (int)
1 1 193 2 2 1 199 210
2 1 263 2 2 1 171 88
3 1 300 3 2 1 164 164
4 1 70 4 2 1 76 80
5 1 65 6 2 2 260 260
6 1 50 3 2 1 171 176
7 1 4 3 2 1 158 167
8 1 29 3 2 1 171 171
9 0 130 1 2 1 161 160
10 0 26 2 2 1 110 110
Run Code Online (Sandbox Code Playgroud)
我尝试过仅使用数值预测器甚至分类预测器。但是两者都没有运气。
任何帮助/指导都将不胜感激。
小智 5
传递一个小数据到data.frame DMwR::SMOTE()将抛出此错误。您可以使用as.data.frame(your_train_data)“取消平铺” data.frame来解决此问题:
trainSM <- SMOTE(conversion ~ ., as.data.frame(train), perc.over = 1000, perc.under = 200)
Run Code Online (Sandbox Code Playgroud)
问题是SMOTE()使用单括号子设置。标题(即data.frame变成tibble::data_frame)对返回值的要求更加严格:单个方括号子集始终返回一个数据帧(即使结果只是单个矢量甚至单个值)。
这是SMOTE()源代码中有问题的部分:
# The idea here is to determine which level of the response variable appears least.
# Unfortunately, if data is a tibble, then data[,tgt] returns a data frame,
# which of course, doesn't have any levels, so the value of minCL is always NULL
minCl <- levels(data[, tgt])[which.min(table(data[, tgt]))]
# this is where the error is thrown--you're testing a data frame against NULL
minExs <- which(data[, tgt] == minCl)
Run Code Online (Sandbox Code Playgroud)