NumPy numpy.rollaxis 函数

  • 简述

    此函数向后滚动指定轴,直到它位于指定位置。该函数接受三个参数。
    
    numpy.rollaxis(arr, axis, start)
    
    参数说明
    序号 参数及说明
    1
    arr
    输入数组
    2
    axis
    轴向后滚动。其他轴的位置相对于彼此不会改变
    3
    start
    默认为零导致完整的滚动。滚动直到到达指定位置
  • 例子

    
    # It creates 3 dimensional ndarray 
    import numpy as np 
    a = np.arange(8).reshape(2,2,2) 
    print 'The original array:' 
    print a 
    print '\n'
    # to roll axis-2 to axis-0 (along width to along depth) 
    print 'After applying rollaxis function:' 
    print np.rollaxis(a,2)  
    # to roll axis 0 to 1 (along width to height) 
    print '\n' 
    print 'After applying rollaxis function:' 
    print np.rollaxis(a,2,1)
    
    它的输出如下 -
    
    The original array:
    [[[0 1]
     [2 3]]
     [[4 5]
     [6 7]]]
    After applying rollaxis function:
    [[[0 2]
     [4 6]]
     [[1 3]
     [5 7]]]
    After applying rollaxis function:
    [[[0 2]
     [1 3]]
     [[4 6]
     [5 7]]]