I'd like to see the top N results for a RandomForestClassifier prediction, ordered by descending probability.

The answer may be predict_proba, but I have no idea how to interpret the results.

Help appreciated!


1 Answer 1


As there isn't too much code/context you're giving us I assume that you're working with a binary classification problem:

import numpy as np
X = ... # input for classification, shape (n_samples, n_features)
y_pred = rf.predict_proba(X)[:, 1] # index slicing to retrieve a 1d-array of probabilities
# array([  0.18, 0.21, 0.1, 0.2, 0.3])

# Now lets see what the 2 biggest probabilities are
# [2, 0, 3, 1, 4] => lowest probability is at index 2 (the 3rd probability as classified by the RandomForestClassifier)
# Now lets see what the top 5 samples look like (remember, argsort sorts in ascending order, so we take the last 5 indices of argsort):

Now you should see the rows in X with the 5 most confident predictions.


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.