Multi-class predictions - output individual probabilities?

Does anyone have code to translate the output values for multi-class prediction to turn those into percentage confidence along with labels?
Basically want to take the % estimate and then add additional processing on top of that to form the final prediction.
Right now I’ve got a bunch of 0/1 and series of very small numbers…would like to output something like:
sapphire band = 01%
diamond band = 88%,
etc. so that can add additional business logic to the prediction based on how confident it is or is not.
Thanks!

If you use learn.get_preds, you should get as first output the probabilities for each class. If you want them as %, just multiply them by 100. If you want the corresponding labels, imagine you have results for one input (results have length N_{classes}) in a variable probs, you can then do something like:

classes = learn.data.classes #or learner.data.train_ds.classes if that doesn't work
class_to_result = [c: p for (c, p) in zip(classes, probs)]

So to wrap all of this:

classes = learn.data.classes
preds, gt = learn.get_preds()
preds = preds.tolist()
confidences = [{c: p for (c, p*100) in zip(classes, probs)} for probs in preds]

Which will be a list of dictionary (one by input), which will be like {'class1': percent1,class2: percent2...}

Hope that helps!

5 Likes

Thanks a ton @florobax - this helps immensely!

The prediction is a tuple of 3 objects. You can call tolist() on the third one like so:
pred[2].tolist().