Reputation: 349
I have two numpy arrays, one of which is 2x2, one of which is 9x9, which both have the same unique value, as tested with np.unique(arr1)==np.unique(arr2).
However, when doing mean on them, the means do not match (the difference is -2.2737367544323206e-13).
I saw in another post that this could be solved by specifying dtype, but in this case it does not matter. The difference is small (2e-13), but for a specific use-case I'd like them to match if there's a simple way to make that happen.
Toy example of same problem:
grid2 = np.ones((2,2))*692.28718729394829309048292902
grid9 = np.ones((9,9))*692.28718729394829309048292902
print(f'Same single unique value: {np.unique(grid2)==np.unique(grid9)}')
mean2 = np.mean(grid2)
mean9 = np.mean(grid9)
print(f'Same mean: {mean2==mean9}')
mean2 = np.mean(grid2,dtype=np.float64)
mean9 = np.mean(grid9,dtype=np.float64)
print(f'Same mean: {mean2==mean9}')
Upvotes: 0
Views: 33
Reputation: 55
This difference is because of floating point arithmetic. There are quite a lot of resources about this online but here is one from the python docs: https://docs.python.org/3/tutorial/floatingpoint.html
If you want to check that the means are almost equal then you can use np.isclose and specify the tolerance: https://numpy.org/doc/stable/reference/generated/numpy.isclose.html
Upvotes: 0