Skip to content Skip to sidebar Skip to footer

Compare Two Numpy Arrays Row-wise Valueerror

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.array([[1,4],[2,3]]) comp= np.logical_and(np

Solution 1:

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]])

Solution 2:

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)

Solution 3:

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

Post a Comment for "Compare Two Numpy Arrays Row-wise Valueerror"