简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM