Compare two numpy arrays row-wise ValueError

后端 未结 3 1200
余生分开走
余生分开走 2021-01-07 04:41

I want to compare two NumPy arrays row-wise and return the number of same rows.

If i use the code below:

a=np.array([[1,2],[3,4]])
b=np.         


        
3条回答
  •  萌比男神i
    2021-01-07 05:31

    I think that you want something akin to:

    np.sum(np.all(np.equal(a, b), axis=1))
    

    which can shorthand to the following if you prefer:

    np.sum(np.all(a == b, axis=1))
    

    This will return 1 for:

    a = np.array([[1, 2], [3, 4]])
    b = np.array([[1, 2], [5, 6]])
    

    but 0 for:

    a = np.array([[1, 2], [3, 4]])
    b = np.array([[1, 3], [5, 6]])
    

提交回复
热议问题