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!
Data Science Stack Exchange is a question and answer site for Data science professionals, Machine Learning specialists, and those interested in learning more about the field. It only takes a minute to sign up.
Sign up to join this communityAs 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
y_pred
# array([ 0.18, 0.21, 0.1, 0.2, 0.3])
# Now lets see what the 2 biggest probabilities are
np.argsort(y_pred)
# [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):
X[np.argsort(y_pred)[-5:]]
Now you should see the rows in X with the 5 most confident predictions.