【问题标题】:SMOTE length of 'dimnames' [2] not equal to array extent'dimnames' [2] 的 SMOTE 长度不等于数组范围
【发布时间】:2016-12-01 15:48:54
【问题描述】:

我试图使用 SMOTE 对我的数据集进行超级采样,但我一直遇到此错误。

trainSM <- SMOTE(conversion ~ ., train,perc.over = 1000,perc.under = 200)

矩阵错误(unlist(value, recursive = 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

我曾尝试仅使用数字预测器甚至分类预测器。但两者都没有运气。

非常感谢任何帮助/指导。

【问题讨论】:

    标签: r sampling


    【解决方案1】:

    将一个 tibble 的 data.frame 传递给 DMwR::SMOTE() 将引发此错误。您可以通过使用 as.data.frame(your_train_data) 来“取消 tibble”您的 data.frame 来解决它:

        trainSM <- SMOTE(conversion ~ ., as.data.frame(train), perc.over = 1000, perc.under = 200)
    

    问题在于SMOTE() 使用单括号子集。 Tibbles(即一个 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 2022-12-08
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多