Compare two numpy arrays row-wise ValueError

后端 未结 3 1179
余生分开走
余生分开走 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:24

    What about something like this:

    import numpy as np
    
    a = np.array([['a', 'b'], ['c', 'd'],\
                      ['e', 't'], ['a', 'b'], ['a', 'b']])
    [['a' 'b']
     ['c' 'd']
     ['e' 't']
     ['a' 'b']
     ['a' 'b']]
    
    b = np.array([['a','b'],['e','t'],['r','t']])
    [['a' 'b']
     ['e' 't']
     ['r' 't']]
    
    shared_rows=0
    
    for row in b:
        temp=a==row
        shared_rows+=sum(np.sum(temp, axis=1)==a.shape[1])
    
    print(shared_rows)
    4
    
    0 讨论(0)
  • 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 "<stdin>", line 1, in <module>
    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)
    
    0 讨论(0)
  • 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]])
    
    0 讨论(0)
提交回复
热议问题