4

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.

パーティーパッケージにも同じものが必要です。入手する方法はありますか?

4

1 に答える 1

0

達成したいことはわかっているが、それを実行するのに十分な 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)
    }
)

こちらからお持ちいただければ幸いです。

于 2014-10-16T00:07:27.997 に答える