【问题标题】:Bincount one-hot encoded labels [duplicate]Bincount one-hot 编码标签 [重复]
【发布时间】:2020-07-18 04:33:56
【问题描述】:

我有一组单热编码标签,我想看看每个类别有多少。每个标签可以包含一个或多个类,如下所示:

[1  0   0   0   0   0   0   1   0]

我对这个问题的第一个解决方案是像这样使用np.argmaxnp.bincount

newLabels = []
for i in range(len(labels)):
    newLabels.append(np.argmax(labels[i]))

newLabels= np.asarray(newLabels)

np.bincount(newLabels)
array([1221,  722,  199,  918,  599,  678, 1569,  786,  185])

但随后发生的情况是,上面的 one-hot 编码示例将被赋予值 0,而第二个值(应该是 7)不计算在内。

有人能解决这个问题吗?

【问题讨论】:

  • 你能分享一个MCVE吗?这不只是列的总和吗?
  • @yatu,你是对的!只是总结每一列

标签: python numpy one-hot-encoding multilabel-classification multiclass-classification


【解决方案1】:

解决这个问题的方法是:

np.sum(Labels, axis=0)

【讨论】:

    【解决方案2】:
    from collections import Counter
    
    newLabels = Counter()
    for label in labels:
        for idx, key in enumerate(label):
             newLabels[idx]+=key
    

    输出应该是一个字典,其中键是标签索引,值是计数。

    【讨论】:

      猜你喜欢
      • 2021-08-05
      • 2021-12-13
      • 2020-11-04
      • 1970-01-01
      • 2020-10-08
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      相关资源
      最近更新 更多