【问题标题】:cforest party unbalanced classescforest 派对不平衡类
【发布时间】:2014-12-11 04:29:10
【问题描述】:

我想使用派对库中的 cforest 函数来衡量特征的重要性。

我的输出变量在 0 类中有 2000 个样本,在 1 类中有 100 个样本。

我认为避免由于类不平衡而产生偏差的一个好方法是使用子样本训练森林中的每棵树,使类 1 的元素数与类 0 中的元素数相同。

有没有办法做到这一点?我正在考虑像n_samples = c(20, 20)这样的选项

编辑: 代码示例

   > iris.cf <- cforest(Species ~ ., data = iris, 
    +                    control = cforest_unbiased(mtry = 2)) #<--- Here I would like to train the forest using a balanced subsample of the data

 > varimp(object = iris.cf)
    Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
     0.048981818  0.002254545  0.305818182  0.271163636 
    > 

编辑: 也许我的问题还不够清楚。 随机森林是一组决策树。通常,决策树仅使用数据的随机子样本构建。我希望使用的子样本在 1 类和 0 类中具有相同数量的元素。

编辑: 我正在寻找的功能肯定在 randomForest 包中可用

sampsize    
Size(s) of sample to draw. For classification, if sampsize is a vector of the length the number of strata, then sampling is stratified by strata, and the elements of sampsize indicate the numbers to be drawn from the strata.

派对包我也需要同样的东西。有没有办法得到它?

【问题讨论】:

    标签: r random-forest party


    【解决方案1】:

    我会假设你知道你想要完成什么,但你不知道足够的 R 来做到这一点。

    不确定该函数是否提供数据平衡作为参数,但您可以手动完成。下面是我快速拼凑的代码。可能存在更优雅的解决方案。

    # just in case
    myData <- iris
    # replicate everything *10* times. Replicate is just a "loop 10 times".
    replicate(10,
        {   
            # split dataset by class and add separate classes to list
            splitList <- split(myData, myData$Species)
            # sample *20* random rows from each matrix in a list
            sampledList <- lapply(splitList, function(dat) { dat[sample(20),] })
            # combine sampled rows to a data.frame
            sampledData <- do.call(rbind, sampledList)
    
            # your code below
            res.cf <- cforest(Species ~ ., data = sampledData,
                              control = cforest_unbiased(mtry = 2)
                              )
            varimp(object = res.cf)
        }
    )
    

    希望你能从这里拿走它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 2020-10-11
      相关资源
      最近更新 更多