pythonnumpy

What is the meaning of numpy reduceat() in python?


I am very new in Python and I read at this moment just the tutorial.

I am confused about the reduceat() function.

I saw the example:

np.add.reduceat([0,1,2,3,4,5,6,7],[0,4,1,5,2,6,3,7])[::2]

and result is:

array([ 6, 10, 14, 18])

How does it come out? Can some one explain for me?


Solution

  • It is sort of like a rolling apply, see:

    In [59]:
    np.add.reduceat([0,1,2,3,4,5,6,7],[0,4])
    Out[59]:
    array([ 6, 22])
    
    In [65]:    
    np.add.reduceat([0,1,2,3,4,5,6,7],[4,1])
    Out[65]:
    array([ 4, 28])
    
    In [66]:
    np.add.reduceat([0,1,2,3,4,5,6,7],[1,5])
    Out[66]:
    array([10, 18])
    
    In [64]:
    np.add.reduceat([0,1,2,3,4,5,6,7],[5,2])
    Out[64]:
    array([ 5, 27])
    
    In [61]:
    np.add.reduceat([0,1,2,3,4,5,6,7],[2,6])
    Out[61]:
    array([14, 13])
    
    In [67]:
    np.add.reduceat([0,1,2,3,4,5,6,7],[6,3])
    Out[67]:
    array([ 6, 25])
    
    In [62]:
    np.add.reduceat([0,1,2,3,4,5,6,7],[3,7])
    Out[62]:
    array([18,  7])
    

    If you want just the 1st value, you can get it done in just one shot:

    In [63]:    
    np.add.reduceat([0,1,2,3,4,5,6,7],[0,4,1,5,2,6,3,7])
    Out[63]:
    array([ 6,  4, 10,  5, 14,  6, 18,  7])