MachineLearningNotebooks icon indicating copy to clipboard operation
MachineLearningNotebooks copied to clipboard

[help wanted] function "model.predict_proba" doesn't give order of classes

Open SleepyRoy opened this issue 7 years ago • 6 comments

This applies to the latest automl (1.0.55).

For a typical classification problem, once an automl experiment is run and a desired model is chosen, we would be able to pass test X data into function model.predict_proba(self, X) to obtain the computed probabilities of each class, just like what is said in help(model):

| predict_proba(self, X) | Apply transforms, and predict_proba of the final estimator |

Parameters
X : iterable
Data to predict on. Must fulfill input requirements of first step
of the pipeline.
Returns
-------
y_proba : array-like, shape = [n_samples, n_classes]

However, the order of classes is not given in returns. For example, assume there's a four-class problem and the computed probabilities for a case are 0.2, 0.25, 0.4, 0.15. The problem is, 0.2/0.25/0.4/0.15 is the probability of which class?

I notice that there's an attribute self.classes_. I guess it's what I want (Unfortunately there's no introduction of it in help()). But then I find that it's simply a useless numpy array - For an m-class classification problem, it's always [0, 1, 2, ..., m-1] rather than names of classes.

Really eager to find the solution cause I'm working on a project in company. Thanks!!!

SleepyRoy avatar Aug 13 '19 17:08 SleepyRoy

@SleepyRoy this is a bug in AutoML. While we are working to fix it, you can try this workaround below: y_map = {i:class_name for i, class_name in enumerate(np.unique(y_test))} print(y_map)

This will give you a mapping of the index of classes with the actual class names. A sample output looks like this: {0: 'ARSON', 1: 'ASSAULT', 2: 'BATTERY', 3: 'BURGLARY', 4: 'CRIM SEXUAL ASSAULT', 5: 'CRIMINAL DAMAGE', 6: 'CRIMINAL TRESPASS', 7: 'DECEPTIVE PRACTICE', 8: 'GAMBLING', 9: 'HOMICIDE', 10: 'INTERFERENCE WITH PUBLIC OFFICER', 11: 'KIDNAPPING', 12: 'MOTOR VEHICLE THEFT', 13: 'NARCOTICS', 14: 'OFFENSE INVOLVING CHILDREN', 15: 'OTHER OFFENSE', 16: 'PROSTITUTION', 17: 'PUBLIC PEACE VIOLATION', 18: 'ROBBERY', 19: 'SEX OFFENSE', 20: 'THEFT', 21: 'WEAPONS VIOLATION'}

ayuwhu avatar Aug 27 '19 01:08 ayuwhu

@ayuwhu Thanks for your reply! It's night here and I'll try it tomorrow. I used to observe some experiments and it seems that the names of classes are always placed in ascending order. For example, if there's a four-class problem (class names: ab, bc, cd, da) and predict_proba returns [0.1, 0.2, 0.3, 0.4], then it seems that 0.1-ab, 0.2-bc, 0.3-cd, 0.4-da. In my current code I just made this assumption. Is it true? You know, I've already run some experiments based on this assumption and if yes, I won't have to repeat them - It'll take a lot of time.

SleepyRoy avatar Aug 27 '19 02:08 SleepyRoy

@SleepyRoy yes, default sorting is alphabetic when we encode the classes.

ayuwhu avatar Aug 27 '19 23:08 ayuwhu

The label encoder used for transforming non-numeric y is available through the y_transformer field of the fitted_model. We use sklearn labelencoder for this. Therefore, you can use inverse_transform() method to obtain the actual label corresponding to the transformed y

Example with finding "max":

for probabilities in fitted_model.predict_proba(X_test):
    # Get maximum probability for this row
    max_prob = max(probabilities)
    # Get index of the class with highest probability
    class_with_best_prob, = np.where(probabilities == max_prob)
    print(fitted_model.y_transformer.inverse_transform(class_with_best_prob)[0], max_prob)

image

skasturi avatar Sep 05 '19 18:09 skasturi

Thank you for your post. We’re glad to see this issue was resolved and will close this thread. Should you have additional questions, please continue to post here and we will gladly continue the discussion.

v-strudm-msft avatar Apr 21 '20 02:04 v-strudm-msft

This should work better

import pandas as pd

output_df = pd.DataFrame(classifer.predict_proba(input_sample), 
             columns=classifer.y_transformer.inverse_transform(classifer.classes_))
output_df.T

datashinobi avatar Sep 23 '20 09:09 datashinobi