【问题标题】:How to insert selected variables by RFE into machine learning model in r?如何将 RFE 选择的变量插入到 r 中的机器学习模型中?
【发布时间】:2019-02-25 15:40:04
【问题描述】:

我想使用递归特征消除方法来选择最上面的特征,然后将它们放入机器学习模型中。我把RFE的代码写成

library(mlbench)
library(caret)
control <- rfeControl(functions=rfFuncs, method="cv", number=10)
results <- rfe(train[,1:134], train[,135], sizes=c(1:134),rfeControl=control)
print(results)
predictors(results)

然后代码为我提供了以下主要功能: [1] “a” “b” “c” “d” “e” 最后我将特征放入模型中:

weighted_fit <- train(x ~ a+b+c+d,
data = train,
method = 'glmnet',
trControl = ctrl)

我的问题是每次 RFE 为我提供 [1] "a" "b" "c" "d" "e" 的顶级功能时,我必须将它们编辑为 a+b+c+d 并将它们放入手动进入模型,但是,当有 50 个特征被选为顶级特征时,无法对其进行编辑并将它们放入模型中,有什么方法可以自动执行此操作。非常感谢您的意见。

【问题讨论】:

    标签: r machine-learning rfe


    【解决方案1】:

    help("update") 是您要找的吗?

    x <- rnorm(10)
    a <- 1:10
    b <- 11:20
    c <- 21:30
    d <- rnorm(10)
    
    fmla <- x ~ a
    
    update(fmla, "~b")
    #x ~ b
    
    new <- c("b", "c", "d")
    update(fmla, paste("~", paste(new, collapse = "+")))
    #x ~ b + c + d
    

    【讨论】:

    • 非常感谢!完美运行。
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多