numpy.roll(array,shift,axis = None):沿著指定的軸滾動數組元素。本質上發生的是輸入數組的元素被移位。如果將元素首先滾動到last-position,則將其回滾到first-position。
參數:
array : [array_like][array_like]Input array, whose elements we want to roll shift : [int or int_tuple]No. of times we need to shift array elements. If a tuple, then axis must be a tuple of the same size, and each of the given axes is shifted by the corresponding number. If an int while axis is a tuple of ints, then the same value is used for all given axes. axis : [array_like]Plane, along which we wish to roll array or shift it's elements.
返回:
Output rolled array, with the same shape as a.
# Python Program illustrating
# numpy.roll() method
import numpy as geek
array = geek.arange(12).reshape(3, 4)
print("Original array : \n", array)
# Rolling array; Shifting one place
print("\nRolling with 1 shift : \n", geek.roll(array, 1))
# Rolling array; Shifting five places
print("\nRolling with 5 shift : \n", geek.roll(array, 5))
# Rolling array; Shifting five places with 0th axis
print("\nRolling with 5 shift with 0 axis : \n", geek.roll(array, 2, axis = 0))
輸出:
Original array : [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] Rolling with 1 shift : [[11 0 1 2] [ 3 4 5 6] [ 7 8 9 10]] Rolling with 5 shift : [[ 7 8 9 10] [11 0 1 2] [ 3 4 5 6]] Rolling with 5 shift with 0 axis : [[ 4 5 6 7] [ 8 9 10 11] [ 0 1 2 3]]
參考文獻:
https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.roll.html
相關用法
注:本文由純淨天空篩選整理自 numpy.roll() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。