Search code examples
pythonpandaslistmachine-learningartificial-intelligence

How to get the probability and label for each class?


I have a model which classifies the venues based on some condition , It has 10 classes and I want that how much model predicts confidence score for each class ?

My code : Result is an array in which model do predictions

        predictions=model.predict(result)
        confidence_score= model.predict_proba(list(result))

model.predict returns only a single value and confidence score has the list of scores for each class as shown below :

[[0.       0.14       0.       0.       0.       0.56       0.       0.17
  0.1      0.01       0.       0.20       0.       0.       0.002    0.01]]

It should return the class label also for each class that eg: Class A has the probability of occurring 0.2 % , etc.

labelencoder.inverse_transform(predictions) 

Output should looks like :

{Class Label A : Probability score , Class Label B: Probability score ....}

Output comes using below code :

      dictionary=[dict(zip(labelencoder.classes_, cs)) for cs in confidence_score]

Output   
{Covent Garden': 0.0, 'London Cocktail Club - Liverpool Street': 0.0,
'Lost Society Battersea': 0.0, 'Lost Society Putney': 0.94.....}

In this case you can see that Lost Society has higher confidence score but when I do model.predict it return me some other label not this one , I have written in the code that predict the class which has highest score .

My code :
        predictions=model.predict(result) //returns the single number 
        confidence_score= model.predict_proba(list(result))

        dictionary=[dict(zip(labelencoder.classes_, cs)) for cs in confidence_score]
        print(dictionary)
            
        print("Recommended Venue for this type of category is",labelencoder.inverse_transform(predictions))
        print("Confidence Score : ", np.max(confidence_score))
        return labelencoder.inverse_transform(predictions),np.max(confidence_score)

Solution

  • We can zip the labelencoder.classes_ and confidence_score and pass the zip object to dict in order to create a dictionary

    dict(zip(labelencoder.classes_, confidence_score.squeeze()))
    

    In case you want to predict multiple samples in one go

    [dict(zip(labelencoder.classes_, cs)) for cs in confidence_score]