将数组更改为整数百分比值

时间:2018-09-02 14:54:12

标签: arrays python-3.x data-conversion

我有一个函数,可以在运行时为我提供一个数组形式的值作为输出,但我需要将输出作为结果块的整数百分比

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

输出结果如下:

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

enter image description here

我的问题是如何获取这两个值显示为:

猫的百分比= 15.81724%

狗%= 84.18274%

1 个答案:

答案 0 :(得分:1)

您可以将zip功能用作:

for z in zip(['Cat', 'Dog'], [15.3, 84.6]):
    print('%s %% = %s%%'%(z[0], z[1]))