Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print mismatch items in two array

I want to compare two array(4 floating point)and print mismatched items. I used this code:

>>> from numpy.testing import assert_allclose as np_assert_allclose
>>> x=np.array([1,2,3])
>>> y=np.array([1,0,3])
>>> np_assert_allclose(x,y, rtol=1e-4)

AssertionError: 
Not equal to tolerance rtol=0.0001, atol=0

(mismatch 33.33333333333333%)
 x: array([1, 2, 3])
 y: array([1, 0, 3])

the problem by this code is with big array:

(mismatch 0.0015104228617559556%)
 x: array([ 0.440088,  0.35994 ,  0.308225, ...,  0.199546,  0.226758,  0.2312  ])
 y: array([ 0.44009,  0.35994,  0.30822, ...,  0.19955,  0.22676,  0.2312 ])

I can not find what values are mismatched. how can see them ?

like image 758
pd shah Avatar asked Aug 16 '17 12:08

pd shah


1 Answers

Just use

~np.isclose(x, y, rtol=1e-4)  # array([False,  True, False], dtype=bool)

e.g.

d = ~np.isclose(x, y, rtol=1e-4)
print(x[d])  # [2]
print(y[d])  # [0]

or, to get the indices

np.where(d)  # (array([1]),)
like image 79
Nils Werner Avatar answered Sep 24 '22 03:09

Nils Werner