【问题标题】:Storing Multiple Models in Loop and save these to compare Variables在循环中存储多个模型并保存这些以比较变量
【发布时间】:2022-10-01 00:50:37
【问题描述】:

我有兴趣使用 RandomForest 作为我的分类问题模型。我已经能够运行一个非常简单的模型进行初始测试。但是,我想尝试使用嵌套循环来运行各种模型并将它们保存到向量中。这主要是为了做到原则上的两个目标:

  1. 从我的循环中提取最佳模型(或者获取这些模型的平均值?)
  2. 比较我的模型之间最重要的变量,看看哪些是每个预测中最常选择的特征。

    我目前正在使用 Iris 数据集进行测试,以查看在应用到具有更多功能(> 100 :) 的更大数据集之前这是多么可行

    嵌套模型示例

    到目前为止,我有以下内容:

    #Set Control
    myControl = trainControl(method = \"cv\", number = 10)
    
    #Set a counter
    myCounter <- 0
    
    RFModel_Vector <- c()
    
    #Nested Loop to select best model
    for (i in 0:2)
    {
        # Train a default Random Forest Model
        RFModel_Vector <- randomForest(y = factor(iris$Species), 
                             x = iris[, colnames(iris) != \"Species\"],
                             importance = TRUE,
                             proximity = TRUE, 
                             trControl = myControl,
                             metric = \"Accuracy\",
                             ntree = 100)
        # Count Number of Loops
        myCounter = counter + 1
        print (myCounter)
    }
    

    我还看到有一个函数caretList 可用于集成方法。

    我不完全确定如何去做。有什么帮助吗?

  • Boruta R 包使用随机森林进行特征选择。

标签: r machine-learning random-forest caret


【解决方案1】:

创建一个list 来存储输出,因为模型输出对象本身就是list,最好将其存储在list

RFModel_Vector <- vector('list', 3)
for (i in seq_along(RFModel_Vector))
{
    # Train a default Random Forest Model
    RFModel_Vector[[i]] <- randomForest(y = factor(iris$Species), 
                         x = iris[, colnames(iris) != "Species"],
                         importance = TRUE,
                         proximity = TRUE, 
                         trControl = myControl,
                         metric = "Accuracy",
                         ntree = 100)
   
}

【讨论】:

    猜你喜欢
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 2013-10-29
    • 2016-03-24
    • 1970-01-01
    相关资源
    最近更新 更多