0

I have a function that gives me a value in the form of an array as an output when run but I need the output as an integer percentage for a results piece

def pred_datsci(file_path):
    prev_precompute = learn.precompute
    learn.precompute = False
    try:
        trn_tfms, val_tfms = tfms_from_model(arch,sz)
        test_img = open_image(file_path)
        im = val_tfms(test_img)
        pred =  learn.predict_array(im[None])
        class_index = (np.exp(pred))
        class_index1 = np.argmax(np.exp(pred))
        print(class_index*100)
        return data.classes[class_index1] 
    finally:
        learn.precompute = prev_precompute

This is what the output looks like:

pred_datsci(f"data/dogscats1/valid/dogs/12501.jpg")

enter image description here

My question is how do I get these two values to display as :

Cat % = 15.81724%

Dog % = 84.18274%

M00N KNIGHT
  • 137
  • 1
  • 11

1 Answers1

1

You can use zip function as:

for z in zip(['Cat', 'Dog'], [15.3, 84.6]):
    print('%s %% = %s%%'%(z[0], z[1]))
sticky bit
  • 36,626
  • 12
  • 31
  • 42
Gang Li
  • 26
  • 2