# Getting both results and probabilities running scikit learn random forest

I have a scikit learn RandomForestClassifier that returns 0s and 1s:

X = [ [2,1,1,1], [2,0,2,1], [3,1,1,1] , [3,1,1,1], [3,1,1,1] ]
y = [ 0, 1, 1, 1, 1 ]

rf = RandomForestClassifier(n_estimators=200, max_depth=5)
rf.fit(X, y)

X_test = [ [2, 0, 1, 0], [2,1,1,1] , [3,1,1,1] ]
y_result = rf.predict(X_test)


I can rerun the classifier and get probabilities instead of values replacing with

y_result = rf.predict_proba(X_test)


But how can I get from scikit learn BOTH the result and the probability?

If I cannot get both results in the same run, does it make sense to run the probability and have a threshold, say 0.7, that if the probability is greater than the threshold then the result is 1 ?

• There is to my knowledge no separate method that returns both the probabilities and final classification, so you'd likely have to run both predict and predict_proba yourself. Jul 23, 2021 at 13:04
• I think I cannot run both and merge the results because the Random Forest will run two random independent processes Jul 23, 2021 at 13:06
• I would expect the same inputs to give the same outputs as long as the model is not refit on the data in between the two calls, but to make sure you could try using the random_state parameters to set the seed. Another option would be to fork the source code and simply add an extra return argument to the predict method since it already calls predict_proba under the hood. Jul 23, 2021 at 13:55
• does it make sense to run predict_proba once and have a threshold, say 0.7, that if the probability is greater than the threshold then the result is X, else is Y? Isn't the 0.7 what's under the hood in predict? Jul 23, 2021 at 14:50
• In predict they are not using a threshold but are simply selecting the class with the highest score, whether that be 0.7 or 0.2. Jul 23, 2021 at 15:24

You can but it's not standard: in binary classification the regular threshold is 0.5 (since if $$p(x)>0.5$$ then $$p(y)<0.5$$). The use of a different threshold makes sense if you want to give more importance to either precision or recall. For example a threshold of 0.7 means requiring higher confidence for positive cases, so less instances predicted as positive therefore higher precision but lower recall.