Skip to content

problem with np.mean on an 2d array compared to same 1d row from it #8116

Closed

Description

@amnona

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

attached is a pickle of x
badx.pickle.zip

Activity

  1. shoyer commented on Oct 4, 2016

    @shoyer
    Member

    This is not unexpected. Numpy's arithmetic is not exact for floating point numbers.

  2. amnona commented on Oct 4, 2016

    @amnona
    Author

    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.

    —
    You are receiving this because you authored the thread.
    Reply to this email directly, view it on GitHub
    #8116 (comment), or mute
    the thread
    https://github.com/notifications/unsubscribe-auth/AFkA8nQwgs-0psIMmQDqFLBQ1eK2DG-Zks5qwdZkgaJpZM4KNM1x
    .

  3. seberg commented on Oct 4, 2016

    @seberg
    Member

    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.

  4. amnona commented on Oct 5, 2016

    @amnona
    Author

    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:

    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.

    —
    You are receiving this because you authored the thread.
    Reply to this email directly, view it on GitHub
    #8116 (comment), or mute
    the thread
    https://github.com/notifications/unsubscribe-auth/AFkA8p6OU-TrptZkITQmv3oGLe7JPSWoks5qwgYWgaJpZM4KNM1x
    .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions