1
$\begingroup$

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 ?

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – Oxbowerce
    Jul 23 at 13:04
  • $\begingroup$ I think I cannot run both and merge the results because the Random Forest will run two random independent processes $\endgroup$
    – ps0604
    Jul 23 at 13:06
  • 1
    $\begingroup$ 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. $\endgroup$
    – Oxbowerce
    Jul 23 at 13:55
  • $\begingroup$ 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? $\endgroup$
    – ps0604
    Jul 23 at 14:50
  • 2
    $\begingroup$ 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. $\endgroup$
    – Oxbowerce
    Jul 23 at 15:24
1
$\begingroup$

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

You can simply run both:

  • The results will always be consistent because there is no randomness involved at the prediction stage, only at training stage.
  • The computations required for predicting are not intensive, so I don't think there can be any major efficiency issue running it twice.

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 ?

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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