Check the number of equal arrays for 2 arrays of arrays

I want to check how many elements of a numpy array in a numpy array are different. The solution should not contain an understanding of the list. Something in these lines (note that a and b are different in the last array):

a = np.array( [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]] )
b = np.array( [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,0,0]] )
y = diff_count( a,b )
print y

>> 1
+4
source share
4 answers

Approach No. 1

Perform a stepwise comparison for non-equality, and then get the reduction ANYalong the last axis and finally count -

(a!=b).any(-1).sum()

Approach # 2

Probably faster with np.count_nonzerofor counting boolean elements -

np.count_nonzero((a!=b).any(-1))

Approach No. 3

Much faster with views-

# https://stackoverflow.com/a/45313353/ @Divakar
def view1D(a, b): # a, b are arrays
    a = np.ascontiguousarray(a)
    b = np.ascontiguousarray(b)
    void_dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
    return a.view(void_dt).ravel(),  b.view(void_dt).ravel()

a1D,b1D = view1D(a,b)
out = np.count_nonzero(a1D!=b1D)

Benchmarking

In [32]: np.random.seed(0)
    ...: m,n = 10000,100
    ...: a = np.random.randint(0,9,(m,n))
    ...: b = a.copy()
    ...: 
    ...: # Let set 10% of rows as different ones
    ...: b[np.random.choice(len(a), len(a)//10, replace=0)] = 0

In [33]: %timeit (a!=b).any(-1).sum() # app#1 from this soln
    ...: %timeit np.count_nonzero((a!=b).any(-1)) # app#2
    ...: %timeit np.any(a - b, axis=1).sum() # @Graipher soln
1000 loops, best of 3: 1.14 ms per loop
1000 loops, best of 3: 1.08 ms per loop
100 loops, best of 3: 2.33 ms per loop

In [34]: %%timeit  # app#3
    ...: a1D,b1D = view1D(a,b)
    ...: out = np.count_nonzero((a1D!=b1D).any(-1))
1000 loops, best of 3: 797 µs per loop
+2
source

, np.ravel(). , .

(a.ravel()!=b.ravel()).sum()
(a-b).any(axis=0).sum()

2 .

, .

(a-b).any(axis=1).sum()

1 .

+1

You can use numpy.anyfor this:

y = np.any(a - b, axis=1).sum()
0
source

Will this work?

y=sum(a[i]!=b[i]for i in range len(a))

Sorry, I can’t check it myself right now.

0
source

Source: https://habr.com/ru/post/1696094/


All Articles