amaresh hiremani
amaresh hiremani

Reputation: 593

how to map the results obtained after multiclass classification to 1 and 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

Upvotes: 0

Views: 509

Answers (1)

Paul Panzer
Paul Panzer

Reputation: 53029

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.

Upvotes: 1

Related Questions