[Python] Why does bottleneck move_mean give different answers for different size arrays but...

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    This may be similar to bottleneck.move_mean get different result for the same ndarray, but that 'question' was ill-posed and so attracted no answers.

    I have found that for a single array, bottleneck move_mean gives different answers depending on whether an average value is extracted from a larger numpy array following averaging, or the average is taken over a subset of the same larger array. This should not be the case. The differences are relatively small but are not zero, as (I think) they should be.

    Test code:

    import numpy as np
    import bottleneck as bn

    howManyIters = 10

    smallDifferences = np.zeros([howManyIters])

    for whichIter in range(howManyIters):

    testArray = np.random.random_sample([512, 353, 53])

    smallDifferences[whichIter] = (bn.move_mean(testArray[:, :, 46:49],
    window=3, axis=2)[0, 41, 2]
    - bn.move_mean(testArray, window=3,
    axis=2)[0, 41, 48])
    print(smallDifferences)


    I get answers like

    [ 1.11022302e-16 4.44089210e-16 2.22044605e-16 2.77555756e-16
    8.32667268e-17 -3.33066907e-16 1.11022302e-16 -5.55111512e-17
    1.66533454e-16 -1.11022302e-16]


    But I expect these to be 0.0. To my understanding, for a window of length 3, the bottleneck windowed averaging is trailing over the previous 2 and the current value, so the slice indexing here should (I hope) match up exactly.

    Continue reading...

Compartilhe esta Página