简体   繁体   English

R中的类概率randomForest

[英]Class probability randomForest in R

I am trying to get the class probability of a binary classification of a randomForest. 我正在尝试获得randomForest的二进制分类的类概率。 I am struggling to get the right syntax. 我正在努力获取正确的语法。 I have tried to read the help file but I have not found the answer. 我试图阅读帮助文件,但没有找到答案。 Any ideas? 有任何想法吗?

> str(training)
'data.frame':   160051 obs. of  5 variables:
 $ repeater           : Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ...
 $ offervalue         : num  0.75 0.75 1.5 0.75 1.25 1.25 1 0.75 0.75 0.75 ...
 $ has_bought_brand   : Factor w/ 2 levels "FALSE","TRUE": 1 1 2 1 1 1 2 1 1 1 ...
 $ has_bought_company : Factor w/ 2 levels "FALSE","TRUE": 1 1 2 1 2 2 2 2 1 1 ...
 $ has_bought_category: Factor w/ 2 levels "FALSE","TRUE": 2 1 1 1 2 2 2 1 1 1 ...

> model <- randomForest(repeater ~ offervalue + has_bought_brand + has_bought_company + has_bought_category, training, ntree=50)

> testPrediction <- predict(model, testing)

> str(testPrediction)
 Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "names")= chr [1:64020] "4" "5" "11" "12" ...

First of all, when posting code, make sure it's reproducible ; 首先,在发布代码时,请确保其可复制 ideally we should be able to copy/paste it into our own R sessions to get the same error/problem as you. 理想情况下,我们应该能够将其复制/粘贴到我们自己的R会话中,以获取与您相同的错误/问题。 Post a str() of a data.set does not help. 发布data.set的str()无济于事。 Often you can find simple examples in the help pages of the functions involved. 通常,您可以在所涉及功能的帮助页面中找到简单的示例。 The following example comes from ?randomForest 以下示例来自?randomForest

set.seed(71)
iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE,
                        proximity=TRUE)

since class(iris.rf)==c("randomForest.formula", "randomForest") , when you call predict(iris.rf) , you're actually calling predict.randomForest() . 由于class(iris.rf)==c("randomForest.formula", "randomForest") ,因此,当您调用predict(iris.rf) ,实际上是在调用predict.randomForest() The help page for ?predict.randomForest gives the documentation for all the various parameters including the type= parameter. ?predict.randomForest的帮助页面提供了所有各种参数(包括type=参数)的文档。 By default it just returns the predicted class, but you can return the predicted probabilities with type="prob" ) 默认情况下,它仅返回预测的类,但是您可以使用type="prob"来返回预测的概率)

predict(iris.rf, type="prob")

which returns 哪个返回

         setosa  versicolor   virginica
1   1.000000000 0.000000000 0.000000000
2   1.000000000 0.000000000 0.000000000
3   1.000000000 0.000000000 0.000000000
4   1.000000000 0.000000000 0.000000000
# etc ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM