Compare two numpy arrays row-wise ValueError

后端 未结 3 1212
余生分开走
余生分开走 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条回答
  •  醉话见心
    2021-01-07 05:25

    Just to extend the answer from @mgilson. You had the right idea, first you did this:

    a = np.array([[1,2],[3,4]])
    b = np.array([[1,4],[2,3]])
    np.equal(a, b)
    >>>array([[ True, False],
       [False, False]], dtype=bool)
    

    Now, you want to pass this to np.logical_and(), which if you look at the docs, it takes in two variables, x1 and x2 (http://docs.scipy.org/doc/numpy/reference/generated/numpy.logical_and.html).

    So if you pass in the above array, you get the following:

    np.logical_and(np.array([[True, False], [False, False]]))
    Traceback (most recent call last):
      File "", line 1, in 
    ValueError: invalid number of arguments
    

    This is because np.array([[True, False], [False, True]]) is a single array, i.e, you only gave an x1 value, and did not give an x2 value. This is why the traceback tells you 'invalid number of arguments'. You need to give two values to this function.

    @zero323 rightly gave you one solution, which is to just unpack the values into the function. More specifically, pass the first array value [True, False] into x1, and [False, False] into x2:

    >>> np.logical_and(*np.equal(a, b))
    array([False, False], dtype=bool)
    

提交回复
热议问题