我正在尝试获取随机森林二元分类的类概率。我正在努力寻找正确的语法。我尝试阅读帮助文件,但没有找到答案。有什么想法吗?
> 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" ...
最佳答案
首先,发布代码时,请确保它是 reproducible ;理想情况下,我们应该能够将其复制/粘贴到我们自己的 R session 中,以获得与您相同的错误/问题。发布 data.set 的 str()
没有帮助。通常,您可以在所涉及功能的帮助页面中找到简单的示例。以下示例来自?randomForest
set.seed(71)
iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE,
proximity=TRUE)
由于 class(iris.rf)==c("randomForest.formula", "randomForest")
,当您调用 predict(iris.rf)
时,您实际上是在调用predict.randomForest()
。 ?predict.randomForest
的帮助页面提供了所有各种参数(包括 type=
参数)的文档。默认情况下,它只返回预测的类别,但您可以使用 type="prob"
) 返回预测的概率
predict(iris.rf, type="prob")
返回
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 ....
关于r - R 中的类概率 randomForest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25850057/