Reverse an arbitrary dimension in an ndarray

后端 未结 3 1136
慢半拍i
慢半拍i 2021-02-20 04:28

I\'m working with an n-dimensional array, and I\'d like a way to reverse a numbered dimension. So rather than

rev = a[:,:,::-1]

I\'d like to be

相关标签:
3条回答
  • 2021-02-20 04:41

    If you browse the numpy (python) source code you'll find a trick they use to write functions that operate on a particular axis is to use np.swapaxes to put the target axis in the axis = 0 position. Then they write code that operates on the 0-axis, and then they use np.swapaxes again to put the 0-axis back in its original position.

    You can do that here like so:

    import numpy as np
    def rev(a, axis = -1):
        a = np.asarray(a).swapaxes(axis, 0)
        a = a[::-1,...]
        a = a.swapaxes(0, axis)
        return a
    
    a = np.arange(24).reshape(2,3,4)
    
    print(rev(a, axis = 2))
    

    yields

    [[[ 3  2  1  0]
      [ 7  6  5  4]
      [11 10  9  8]]
    
     [[15 14 13 12]
      [19 18 17 16]
      [23 22 21 20]]]
    
    0 讨论(0)
  • 2021-02-20 04:41

    Turns out this can be done with slice, for which : is shorthand in some contexts. The trick is to build the index object as a tuple of slices:

    import numpy as np
    
    def reverse(a, axis=0): 
        idx = [slice(None)]*len(a.shape)
        idx[axis] = slice(None, None, -1)
        return a[idx]
    
    a = np.arange(24).reshape(2,3,4)
    print reverse(a, axis=2)
    

    With Ellipsis this can be collapsed into a one-liner:

    a[[slice(None)]*axis + [slice(None, None, -1)] + [Ellipsis]]
    
    0 讨论(0)
  • 2021-02-20 04:42

    For anyone coming across this in the future:

    Numpy 1.12+ has the function np.flip(array, dimension), which does exactly as requested. Even better, it returns a view of the data rather than a copy, and so it happens in constant time.

    0 讨论(0)
提交回复
热议问题