==
with dictionaries compares keys and values. But the values are arrays. array1 == array2
produces a boolean array, which does not play well with the yes/no expectations of the dictionary test (the ambiguity error).
A way around that is to compare the values individually. np.allclose
is the best test for float arrays. Assuming that the keys match, the following list comprehension works nicely:
In [177]: array=np.array
In [178]: a = {0: array([4.5, 5. ]), 1: array([3.5, 4.5]), 2: array([1., 1.])}
...: b = {0: array([4., 5. ]), 1: array([3, 4]), 2: array([1.5, 1.])}
In [179]: a
Out[179]: {0: array([4.5, 5. ]), 1: array([3.5, 4.5]), 2: array([1., 1.])}
In [180]: b
Out[180]: {0: array([4., 5.]), 1: array([3, 4]), 2: array([1.5, 1. ])}
In [181]: [np.allclose(a[k],b[k]) for k in a]
Out[181]: [False, False, False]
In [182]: [np.allclose(a[k],a[k]) for k in a]
Out[182]: [True, True, True]
There should be another lay of testing, for equal keys.
However allclose
does not work if the arrays differ in shape:
In [183]: c = {0: array([4., 5., 0 ]), 1: array([3, 4]), 2: array([1, 1.])}
In [185]: [np.allclose(a[k],c[k]) for k in a]
....
ValueError: operands could not be broadcast together with shapes (2,) (3,)
The comparison task will be a lot simpler if you know how the dictionaries might differ. Can they differ in keys? Can they differ in types of the values (array vs a list vs a number)? If values are arrays, can they differ in shape?