Reputation: 841
I noticed that the output of numpy.mean()
and numpy.var()
changes as the ordering of its arguments change.
It has to do only with the precision of floats, or I'm missing something?
import numpy
l1 = [1.0, 0.69, 0.65, 0.7, 0.64, 0.8]
l2 = [1.0, 0.7, 0.69, 0.65, 0.64, 0.8]
assert sorted(l1) == sorted(l2)
print repr(numpy.mean(l1))
print repr(numpy.mean(l2))
print repr(numpy.var(l1))
print repr(numpy.var(l2))
Here the output that I obtain:
0.7466666666666667
0.74666666666666659
0.015522222222222222
0.015522222222222224
Upvotes: 1
Views: 72
Reputation: 85442
NumPy uses (in this case) 64-bit floats. Floating point numbers cannot represent all numbers with unlimited precision.
This data type can hold 15 significant digits. This means the last digit in your number is meaningless.
Therefore, to compare our numbers use numpy.allclose
:
>>> numpy.allclose(numpy.mean(l1), numpy.mean(l2))
True
Upvotes: 3