user4532296
user4532296

Reputation:

Numpy: how to get the items that are different of 2 matrices?

I know how to compare 2 matrices of integers using Numpy. But does Numpy offer a way to get the list of elements that are different between them ?

Upvotes: 1

Views: 73

Answers (1)

user3684792
user3684792

Reputation: 2611

Something like this?

>>> import numpy as np
>>> a = np.zeros(3)
>>> b = np.zeros(3)
>>> a[1] = 1
>>> a == b
array([ True, False,  True], dtype=bool)

for floats: http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.isclose.html

>>> np.isclose(a,b)
array([ True, False,  True], dtype=bool)

indices of different elements

 >>>[i for i,val in enumerate(np.isclose(a,b)) if val == False]

(using numpy instead)

>>> np.where(np.isclose(a,b) == False)

find values of different elements:

 >>> d = [i for i,val in enumerate(np.isclose(a,b)) if val == False]
 >>> a[d]
 array([ 1.])
 >>> b[d]
 array([ 0.])

Upvotes: 2

Related Questions