Predicted probabilities in R ranger package

我怕爱的太早我们不能终老 提交于 2020-05-14 19:55:09

问题


I am trying to build a model in R with random forest classification. (By editing the code by Ned Horning) I first used randomForest package but then found ranger, which promises faster calculations.

At first, I used the code below to get predicted probabilities for each class at the end of the model with randomForest as:

predProbs <- as.data.frame(predict(randfor, imageBlock, type='prob'))

The type of probability here is as follows:

We have 500 trees in the model and 250 of them says the observation is class 1, hence the probability is 250/500 = 50%

In ranger, I realized that there is not a type = 'prob' option.

I searched and tried some adjustments but couldn't get any progress. I need an object or so containing probabilities as mentioned above with ranger package.

Could anyone give some advice about the issue?


回答1:


You need to train a "probabilistic classifier"-type ranger object:

library("ranger")
iris.ranger = ranger(Species ~ ., data = iris, probability = TRUE)

This object computes a matrix (n_samples, n_classes) when used in the predict.ranger function:

probabilities = predict(iris.ranger, data = iris)$predictions


来源:https://stackoverflow.com/questions/55654644/predicted-probabilities-in-r-ranger-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!