Home > Software engineering >  How to get percentage prediction for every class in multi-classification model
How to get percentage prediction for every class in multi-classification model

Time:06-04

I have trained a sequential model for 10 classes.

classes = ['Begin', 'Choose', 'Connection', 'Navigation',
            'Next', 'Previous', 'Start', 'Stop', 'Hello', 'Web']

Model output:

out = loaded_model.predict(image_sequence)
print(out)
[[5.3001270e-02 1.8615163e-05 3.5275782e-05 1.8662749e-02 3.3778408e-01
  3.2624280e-01 2.6934301e-02 2.2875501e-03 2.3302139e-01 2.0118954e-03]]

I want to get the prediction percentage for each class like this:

Begin: 80%
Choose: 10%
Connection: 6%
and so on.....

CodePudding user response:

classes = ['Begin', 'Choose', 'Connection', 'Navigation',
            'Next', 'Previous', 'Start', 'Stop', 'Hello', 'Web']

out = [5.3001270e-02, 1.8615163e-05, 3.5275782e-05, 
      1.8662749e-02, 3.3778408e-01,  3.2624280e-01, 
      2.6934301e-02, 2.2875501e-03, 2.3302139e-01, 
      2.0118954e-03]

percentage_list = []
for i in range(len(out)):
    percentage_list.append("{0:.2%}".format(out[i]))
    
my_result = list(zip(classes, percentage_list))
for i in range(len(my_result)):
    print(my_result[i])

Result:

('Begin', '5.30%')
('Choose', '0.00%')
('Connection', '0.00%')
('Navigation', '1.87%')
('Next', '33.78%')
('Previous', '32.62%')
('Start', '2.69%')
('Stop', '0.23%')
('Hello', '23.30%')
('Web', '0.20%')
  • Related