【问题标题】:R rfe function "caret" Package error: there should be the same number of samples in x and yR rfe 函数 "caret" 包错误:x 和 y 中的样本数应该相同
【发布时间】:2015-08-07 03:07:39
【问题描述】:

当我尝试来自here 的“caret”包中的 rfe 示例时,我不断收到此错误

  Error in rfe.default(d[1:2901, ], c(1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3,  : 
  there should be the same number of samples in x and y

这个问题has been asked 但它的解决方案不适用于这种情况。

代码如下:

set.seed(7)
# load the library
library(mlbench)
library(caret)

# load the data
d <- read.table("d.dat")

# define the control using a random forest selection function
control <- rfeControl(functions=rfFuncs, method="cv", number=10)

# run the RFE algorithm
results <- rfe(d[1:2901, ],   c(1,1,1,1, 1, 1,2,2,2, 3 ,3,3,4, 4, 4),   sizes=c(1:2901), rfeControl=control)

# summarize the results
print(results)

数据集是一个包含 2901 行(特征)和 15 列的数据框。向量 c(1,1,1,1,1,1,2,2,2,3,3,3,4,4,4) 是特征的预测变量。

我设置错了什么参数?

【问题讨论】:

标签: r r-caret rfe


【解决方案1】:

有一个约定,行是观察值,列是特征。您向rfe 提供 x 参数的方式意味着您有 2901 个观察值,这会产生与 15 个结果不匹配的结果。对您的数据使用转置函数t(如果它当然有 15 列)。

y = c(1,1,1...) 向量不应被称为预测器。它是因变量结果。第一个参数是预测变量的 data.frame。

【讨论】:

    【解决方案2】:

    我们不知道您的数据,但这适用于模拟数据:

    set.seed(7)
    d=data.frame(matrix(rnorm(2901*15,1,.5),ncol=15))
    #something like dependent variable
    dp=factor(sample(c(1,1,1,1, 1, 1,2,2,2, 3 ,3,3,4, 4, 4),2901,replace = TRUE))
    
    # define the control using a random forest selection function
    control <- rfeControl(functions=rfFuncs, method="cv", number=10)
    
    # run the RFE algorithm
    sz=50 # Change sz to 2901 for full sample
    results <- rfe(d[1:sz, ],   dp[1:sz],   sizes=c(1:15), rfeControl=control)
    
    # summarize the results
    print(results)
    
    ## End of the printed results
    ## The top 5 variables (out of 6):
    ##   X5, X6, X15, X14, X3
    

    【讨论】:

      【解决方案3】:
      rfe(x, y,sizes = subsets, rfeControl = ctrl)
      

      您的问题是您没有 x 的 nr 行与向量 y 的长度相同

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-13
        • 1970-01-01
        • 2015-10-29
        • 2013-08-17
        • 2018-03-16
        • 2021-08-18
        相关资源
        最近更新 更多