代码之家  ›  专栏  ›  技术社区  ›  spotHound

两个矩阵之间有多少行等于numpy[重复]

  •  0
  • spotHound  · 技术社区  · 6 年前

    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中有多少行是相等的)

    sum(torch.eq(A,B, axis=0)) # that doesn't exists
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   sacuL    6 年前

    你可以用 numpy 具有 all :

    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 是两个数组之间的精确匹配。

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