gpt4 book ai didi

r - cforest party 不平衡类

转载 作者:行者123 更新时间:2023-12-03 21:39:41 25 4
gpt4 key购买 nike

我想用 party 库中的 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 的了解还不够。

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

# 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)
}
)

希望你能从这里开始。

关于r - cforest party 不平衡类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26393675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com