2
$\begingroup$

We know that randomForest is a classification model. Let's say I have 2 classes A, B , and after run randomForest model (particularly I used R, but it does not matter).

Assume that I have a result:

  A B
A 29 2
B 3 40

The columns are prediction and rows are actual classes.

My question is, could I get the matching score of each element. For instance, for 32 elements which are predicted 'A', could I rank them based on matching score, from the element that is predicted highest match to A, to the lowest match.

(randomForest predicted 32 items as class A, and 42 items as class B, but it does not tell me how much score it gives for each item. For instance, I hope that I can have some information like: the model predict item_1 with 98% belongs to class A, and item_2 with 90% belongs to class A, and so on)

$\endgroup$

1 Answer 1

2
$\begingroup$

You can use votes, i.e., the per cent of trees in the forest that voted for a given class. OOB votes are stored in the $votes element of the randomForest object:

> randomForest(Species~.,data=iris)$votes
         setosa  versicolor   virginica
1   1.000000000 0.000000000 0.000000000
 ...
15  0.979274611 0.020725389 0.000000000
 ...
58  0.005780347 0.745664740 0.248554913

In case of predicting new data, use type="votes" switch in the predict method.

$\endgroup$
2
  • $\begingroup$ The answer is great @mbq, but is there a way when I get the votes for the predicted class (for instance, in your example, the element 1, I just want to get the vote for setosa, so I will have a dataset like: 1 setosa 1.00 ... 15 setosa 0.979 .... 58 versicolor 0.746 $\endgroup$
    – mamatv
    Commented Nov 18, 2015 at 10:05
  • $\begingroup$ That's just max of each row, apply(<votes array>,1,max) will do the trick. You can use standard prediction to append predicted class name. Note that tie-breaking may be inconsistent, but it is very rare. $\endgroup$
    – user88
    Commented Nov 18, 2015 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.