0
$\begingroup$

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!

$\endgroup$

1 Answer 1

1
$\begingroup$

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
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.

$\endgroup$

Your Answer

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

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