You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
problem with np.mean on an 2d array compared to same 1d row from it #8116
When calculating a=np.mean(x,axis=1) on an array and taking one row (i.e. a[10]), there is a very small (1E-16) difference compared to calculating np.mean(x[10,:]) on this one row directly.
example (x is an (1462,54) float64 array):
pita = x[370, :].mean()
pata = np.mean(x, axis=1)[370]
pita - pata
Out[771]: 2.2204460492503131e-16
That's true, but in theory the mean calculation should be identical if done
on all rows (each row should be separate) or on one row at a time... Maybe
the mean on all rows (axis=1) does it via matrix multiplication?
Anyway, maybe worth adding a note in the documentation (similar to the
warning about calculating a mean with float64).
On Mon, Oct 3, 2016 at 9:30 PM, Stephan Hoyer [email protected]
wrote:
This is not unexpected. Numpy's arithmetic is not exact for floating point
numbers.
There are two things here. First numpy tries to be fast, that alone might be enough to change some order (or FPU register usage, though not sure here) which is enough for such difference.
Then we do have more exact summing (pairwise) if it is along the fast axis in memory specifically. This would always be the case in the second case, it may be in the first case.
You have to expect differences like this. There might even be larger differences for very large sums (or means) in some cases especially depending on memory order where summing along the fast axis is potentially more exact.
That makes sense. Thanks!
Maybe just worth adding a note somewhere in the numpy documentation? (I was
using the mean in a permutation test and this small unexpected difference
wrecked havoc on the results..)
There are two things here. First numpy tries to be fast, that alone might
be enough to change some order (or FPU register usage, though not sure
here) which is enough for such difference.
Then we do have more exact summing (pairwise) if it is along the fast axis
in memory specifically. This would always be the case in the second case,
it may be in the first case.
You have to expect differences like this. There might even be larger
differences for very large sums (or means) in some cases especially
depending on memory order where summing along the fast axis is potentially
more exact.
Activity
shoyer commentedon Oct 4, 2016
This is not unexpected. Numpy's arithmetic is not exact for floating point numbers.
amnona commentedon Oct 4, 2016
That's true, but in theory the mean calculation should be identical if done
on all rows (each row should be separate) or on one row at a time... Maybe
the mean on all rows (axis=1) does it via matrix multiplication?
Anyway, maybe worth adding a note in the documentation (similar to the
warning about calculating a mean with float64).
On Mon, Oct 3, 2016 at 9:30 PM, Stephan Hoyer [email protected]
wrote:
seberg commentedon Oct 4, 2016
There are two things here. First numpy tries to be fast, that alone might be enough to change some order (or FPU register usage, though not sure here) which is enough for such difference.
Then we do have more exact summing (pairwise) if it is along the fast axis in memory specifically. This would always be the case in the second case, it may be in the first case.
You have to expect differences like this. There might even be larger differences for very large sums (or means) in some cases especially depending on memory order where summing along the fast axis is potentially more exact.
amnona commentedon Oct 5, 2016
That makes sense. Thanks!
Maybe just worth adding a note somewhere in the numpy documentation? (I was
using the mean in a permutation test and this small unexpected difference
wrecked havoc on the results..)
Thanks again
Amnon
On Tue, Oct 4, 2016 at 12:53 AM, seberg [email protected] wrote: