3

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.equal(a,b))
correct=numpy.sum(comp)

I get the following error:

ValueError: invalid number of arguments

However, this works:

np.logical_and([True, False], [False, False])

This is probably very silly but I am new to NumPy. Please help.

5
  • 1
    do you mean np.array([[1,2], [3,4]])? Commented Aug 9, 2015 at 20:07
  • @jonnybazookatone Yes! Sorry for the mistake. Commented Aug 9, 2015 at 20:08
  • 3
    np.logical_and(*np.equal(a,b))? Commented Aug 9, 2015 at 20:10
  • beat me too it on comments @zero323, write the soln and he can accept as answer ;( Commented Aug 9, 2015 at 20:11
  • You can also print(np.logical_and.reduce(np.equal(a, b)).sum()) Commented Aug 9, 2015 at 21:02

3 Answers 3

15

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]])
Sign up to request clarification or add additional context in comments.

1 Comment

np.logical_and does an element-by-element and for 2 arrays. You're only passing it a single array (hence the ValueError).
3

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)

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.