Reputation: 319
I have 2 numpy arrays with the same shape. Now I want to compare the values of array 1 with array 2. Finally, the sum of the matches with the condition value == 1 is to be output.
array 1:
[1, 0, 1]
[1, 1, 0]
[0, 0, 1]
array 2:
[0, 1, 1]
[1, 0, 0]
[0, 1, 1]
The result should now look like this:
sum = 3
My following algorithm is working but isn't very performant and elegant:
k = 0
for i in range(0,array1.shape[0],1):
for j in range(0, array1.shape[1], 1):
if array1[i][j] == array2[i][j] and array1[i][j] == 1:
k = k + 1
else:
continue
print k
Upvotes: 1
Views: 3728
Reputation: 53079
Here are four more fast methods:
1) np.dot
:
np.dot(a.ravel(), b.ravel())
# 3
2) np.bincount
:
np.bincount(a.ravel(), b.ravel())[1]
# 3.0
3) np.count_nonzero
:
np.count_nonzero(a * b)
# 3
4) np.repeat
:
np.count_nonzero(a.ravel().repeat(b.ravel()))
# 3
Borrowing @IMCoins' test script I get
Test 1 gives : 6.34505105019
Test 2 gives : 7.32884097099
Test 3 gives : 3.29451298714
Test 4 gives : 3.76608014107
Test 5 gives : 1.28572297096 <- np.dot
Test 6 gives : 1.3145699501 <- np.bincount
Test 7 gives : 0.984617948532 <- np.count_nonzero
Test 8 gives : 1.06798291206 <- np.repeat
If I use larger arrays (np.tile(arr[1/2], (100, 100))
) and fewer iterations this becomes:
Test 1 gives : 0.253939151764
Test 2 gives : 34.9494478703
Test 3 gives : 0.190597057343
Test 4 gives : 0.139708995819
Test 5 gives : 0.0751769542694
Test 6 gives : 0.382376909256
Test 7 gives : 0.239590883255
Test 8 gives : 0.335343122482
Upvotes: 1
Reputation: 3306
I made some timings of the above solutions...
1st place : np.logical_and()
2nd place : np.sum(np.multiply())
3nd place : np.sum(arr1 == arr2 & arr1 == 1)
4th place : Basic loops and comparisons.
This will output :
# Test 1 gives : 15.1819742985
# Test 2 gives : 14.9471218792
# Test 3 gives : 6.76537855828
# Test 4 gives : 9.16029915098
import numpy as np
import timeit
arr1 = np.array([[1, 0, 1], [1, 1, 0], [0, 0, 1]])
arr2 = np.array([[0, 1, 1], [1, 0, 0], [0, 1, 1]])
numberIterations = 1000000
def test_1(arr1, arr2):
return np.sum((arr1 == arr2) & (arr1 == 1))
def test_2(arr1, arr2):
summed = 0
for a1, a2 in zip(arr1, arr2):
for c1, c2 in zip(a1, a2):
if c1 == c2 and c1 == 1:
summed += 1
return summed
def test_3(arr1, arr2):
return np.logical_and(arr1, arr2).sum()
def test_4(arr1, arr2):
return np.sum(np.multiply(arr1, arr2))
# Testing time.
# Test 1
time1 = timeit.timeit(stmt='test_1(arr1, arr2)',
setup='from __main__ import test_1, arr1, arr2',
number=numberIterations)
# Test 2
time2 = timeit.timeit(stmt='test_2(arr1, arr2)',
setup='from __main__ import test_2, arr1, arr2',
number=numberIterations)
# Test 3
time3 = timeit.timeit(stmt='test_3(arr1, arr2)',
setup='from __main__ import test_3, arr1, arr2',
number=numberIterations)
# Test 4
time4 = timeit.timeit(stmt='test_4(arr1, arr2)',
setup='from __main__ import test_4, arr1, arr2',
number=numberIterations)
# Printing results.
print 'Test 1 gives : {}'.format(time1)
print 'Test 2 gives : {}'.format(time2)
print 'Test 3 gives : {}'.format(time3)
print 'Test 4 gives : {}'.format(time4)
Upvotes: 2
Reputation: 14399
If it's truly just binary data, you can do
np.logical_and(a, b).sum()
3
This should be much faster than doing (a==b) & (a==1)
Upvotes: 4
Reputation: 1281
If you just have 0 and 1 in your matrix
import numpy as np
ar_1=np.array([[1, 0, 1],
[1, 1, 0],
[0, 0, 1]])
ar_2 = np.array([[0, 1, 1],
[1, 0, 0],
[0, 1, 1]])
np.sum(np.multiply(ar_1,ar_2))
np.multiply will be element wise multiplication. Will give you 1 only if both the arguments given are one.
np.sum without axis will sum all the elements of the matrix
Upvotes: 1
Reputation: 164773
This is one way to combine your 2 conditions.
import numpy as np
a = np.array([[1, 0, 1],
[1, 1, 0],
[0, 0, 1]])
b = np.array([[0, 1, 1],
[1, 0, 0],
[0, 1, 1]])
res = np.sum((a==b) & (a==1)) # 3
Explanation
a==b
returns a Boolean array testing equality between the 2 arrays by element.a==1
returns a Boolean array testing equality of elements within a
to 1 by element.&
operator is used to signify both conditions must be met.np.sum
works on a Boolean array since bool
is a subclass of int
, i.e. True
can be considered as 1, False
as 0.Upvotes: 4
Reputation: 758
>>> import numpy as np
>>> a=np.array([[1,0,1],[1,1,0],[0,0,1]])
>>> b=np.array([[0,1,1],[1,0,0],[0,1,1]])
>>> a==b
array([[False, False, True],
[ True, False, True],
[ True, False, True]], dtype=bool)
>>> c=np.array(a==b, dtype=int)
>>> c
array([[0, 0, 1],
[1, 0, 1],
[1, 0, 1]])
>>> np.sum(c)
5
>>> d=np.array(a*b, dtype=int)
>>> d
array([[0, 0, 1],
[1, 0, 0],
[0, 0, 1]])
>>> np.sum(d)
3
Upvotes: 1