这段代码使用random库随机选择测试样本,使用matplotlib库展示图像,并使用numpy库计算one-hot编码。

for i in range(20):
    rand_num = random.randint(1,1000)
    label = np.where(y_test[rand_num])[0][0]
    #计算one-hot
    pixels = x_test[rand_num]
    pixels = pixels.reshape((128,128,3))
    plt.title('Label is' + str(label))
    plt.imshow(pixels,cmap='gray')
    plt.show()
    print('测试样本第{}个,预测结果是{}'.format(rand_num,classes_x[rand_num]))
    if classes_x[rand_num] != label:
        print('预测错误')

修改预测结果标签

如果你想修改预测结果的标签,可以直接将classes_x[rand_num]的值修改为正确的标签即可。比如:

if classes_x[rand_num] != label:
    print('预测错误')
    classes_x[rand_num] = label

这段代码会将预测错误的样本的标签修改为正确的标签。

Python代码:随机展示测试样本并判断预测结果

原文地址: https://www.cveoy.top/t/topic/oua4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录