【问题标题】:How to create a histogram of a nn model's prediction如何创建 nn 模型预测的直方图
【发布时间】:2021-09-27 11:53:03
【问题描述】:

我的 NN 模型预测图像的标签和分数(参见示例图像,分数是图像下方的浮点数,标签是右侧的属性)

我想制作一个直方图来显示哪些属性(标签)被预测得更多。所以我想索引标签并找到它们的数量,但我不知道该怎么做。

我将所有输出附加到数组 (preds_array) 中,attributes 是标签数组,然后我这样做了:

outputs_array = [j for i in zip(preds_array, attributes) for j in i] #zip the output scores with their labels



#The outputs_array looks like this#
    [0.21103, array(['dirty'], dtype=object), 0.99764, array(['daylight'], dtype=object), 0.000802, array(['night'], dtype=object)

%matplotlib inline

np.random.seed(42)

plt.hist(outputs_array, density=False, bins=30)
plt.ylabel('Count')
plt.xlabel('Predicted Labels');

但它返回一个错误。 有什么想法吗?

【问题讨论】:

    标签: python arrays matplotlib histogram


    【解决方案1】:

    您想要计算每个标签的预测次数?

    import matplotlib.pyplot as plt
    
    attributes = ['a','b','c','a']
    
    predictions_dict ={}
    
    for attr in attributes:
       if attr in predictions_dict.keys():
           predictions_dict[attr] += 1
       else:
           predictions_dict[attr] = 1
    
    
    plt.bar(predictions_dict.keys(), predictions_dict.values())
    plt.show()
    

    如果您想要每个标签的分数直方图,我会推荐类似的东西,但是,根据您上面的内容,看起来您只有每个类是预测类时的分数。但是,如果这是您想要的,那么您可以将上面的内容转换为每个标签的这些分数的直方图

    import matplotlib.pyplot as plt
    import numpy as np
    
    #making up arrays of labels and scores because I don't know all of yours
    labels = ['a','b','c']
    attributes = np.random.choice(labels, 100)
    scores = np.random.random(100)
    
    d = {key:[] for key in labels}
    
    for attribute, score in zip(attributes, scores):
        d[attribute].append(score)
    
    for attribute in d.keys():
        plt.hist(d[attribute], alpha=0.3, label=attribute)
    
    plt.xlabel('score')
    plt.ylabel('count')
    plt.legend()
    plt.show()
    

    【讨论】:

    • 如果您提前知道所有标签,您可以将字典预设为所有键作为标签,值作为 0,这样您的绘图将包含从未预测过的标签。它还将简化循环,因为您不需要 if else 语句。如果 attributes 是 pytorch 张量,您可能还必须使用 .item() 或 .cpu() 来获取标签
    • 我认识他们。我有一个标签数组(属性数组)和一个分数,这是每个标签的预测。我想我必须先把它们结合起来,因为它们是分开的。并有一个带有预测分数及其标签的数组。我在示例中展示的是在训练之后迭代整个数据集。
    • 我已经编辑了上面的内容来做同样的事情,但是为了分数。我认为我所说的分数是你问题中的 preds_array。
    猜你喜欢
    • 2015-10-22
    • 2017-07-27
    • 2022-11-12
    • 2023-03-16
    • 2018-12-29
    • 2011-06-21
    • 2018-04-16
    • 2022-01-21
    相关资源
    最近更新 更多