【问题标题】:Change an array to an integer percentage value将数组更改为整数百分比值
【发布时间】:2018-09-02 14:54:12
【问题描述】:

我有一个函数,它在运行时给我一个数组形式的值作为输出,但我需要将输出作为结果片段的整数百分比

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")

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

猫 % = 15.81724%

狗 % = 84.18274%

【问题讨论】:

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


    【解决方案1】:

    你可以使用 zip 函数:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多