0

I am working on image classification for cifar data set.I obtained the predicted labels as output mapped from 0-1 for 10 different classes is there any way to find the class the predicted label belongs?

//sample output obtained
array([3.3655483e-04, 9.4402254e-01, 1.1646092e-03, 2.8560971e-04,
       1.4086446e-04, 7.1564602e-05, 2.4985364e-03, 6.5030693e-04,
       3.4783698e-05, 5.0794542e-02], dtype=float32)

One way is to find the max and make that index as 1 and rest to 0.

 //for above case it should look like this
 array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0])

can anybody tell me how to do this or else if you have any better methods please suggest. thanks

amaresh hiremani
  • 533
  • 1
  • 8
  • 19

1 Answers1

1

It is as simple as

>>> data = np.array([3.3655483e-04, 9.4402254e-01, 1.1646092e-03, 2.8560971e-04,
...        1.4086446e-04, 7.1564602e-05, 2.4985364e-03, 6.5030693e-04,
...        3.4783698e-05, 5.0794542e-02], dtype=np.float32)
>>> 
>>> (data == data.max()).view(np.int8)
array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)

Explanation: data.max() finds the largest value. We compare that with each individual element to get a vector of truth values. This we then cast to integer taking advantage of the fact that True maps to 1 and False maps to 0.

Please note that this will return multiple ones if the maximum is not unique.

Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
  • we don't know every time predicted values will be unique even though the probability of getting same number is less . In case if they are not unique can you suggest other techniques. By the way thanks for answer – amaresh hiremani Apr 07 '18 at 06:40
  • @amareshhiremani Well, if the max is not unique, it means your classifier has no preference between the classes attaining the max. It's for you to decide what to do. For some followup analyses it may be acceptable to toss a coin, for some it may not. – Paul Panzer Apr 07 '18 at 06:59