【问题标题】:How many rows are equals between two matrices in numpy [duplicate]numpy中两个矩阵之间有多少行相等[重复]
【发布时间】:2019-04-22 07:34:53
【问题描述】:

我正在为多标签分类问题训练神经网络。我有两行 A 和 B,大小为 BxL(B = 批量大小,L = 标签数),其中 A 是小批量的实际标签,B 是我的模型做出的预测,类似这样:

A = array([[0., 1., 0.],
          [0., 1., 1.],
          [0., 1., 0.],
          [0., 0., 0.]])

B = array([[1., 1., 0.],
          [0., 1., 1.],
          [0., 0., 0.],
          [0., 1., 0.]])

我想计算有多少样本被正确分类(也就是说,A 和 B 中有多少行是相等的)

我想知道是否有办法使用 tensor/numpy 并行函数...

类似

sum(torch.eq(A,B, axis=0)) # that doesn't exists

【问题讨论】:

  • 你能添加预期的答案吗(我猜是一个?)

标签: python numpy matrix torch


【解决方案1】:

您可以将numpyall 一起使用:

np.sum((A == B).all(1))
#1

这通过搜索每行中的所有值是否匹配来工作。

>>> A == B
array([[False,  True,  True],
       [ True,  True,  True],
       [ True, False,  True],
       [ True, False,  True]])

为您提供元素匹配的位置,然后 all(axis=1) 返回所有值为 True 的行的布尔值:

>>> (A == B).all(1)
array([False,  True, False, False])

显示索引1 处的行是两个数组之间的完全匹配。

然后,对布尔数组求和即可得到此类行的计数。

【讨论】:

  • 这正是我想要的。没想到,非常感谢,
猜你喜欢
  • 1970-01-01
  • 2019-03-08
  • 2021-03-07
  • 1970-01-01
  • 2017-05-10
  • 1970-01-01
  • 2018-11-19
  • 2017-01-02
  • 1970-01-01
相关资源
最近更新 更多