當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python dask.array.roll用法及代碼示例

用法:

dask.array.roll(array, shift, axis=None)

沿給定軸滾動數組元素。

此文檔字符串是從 numpy.roll 複製的。

可能存在與 Dask 版本的一些不一致之處。

超出最後一個位置的元素首先重新引入。

參數

a數組(在 Dask 中不支持)

輸入數組。

shift整數或整數元組

元素移動的位置數。如果是元組,那麽 axis 必須是相同大小的元組,並且每個給定的軸移動相應的數字。如果一個 int while axis 是一個 int 元組,則所有給定軸使用相同的值。

axis整數或整數元組,可選

元素沿其移動的一個或多個軸。默認情況下,數組在移動之前會被展平,之後會恢複原始形狀。

返回

resndarray

輸出數組,形狀與 a 相同。

注意

支持同時滾動多個維度。

例子

>>> x = np.arange(10)  
>>> np.roll(x, 2)  
array([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])
>>> np.roll(x, -2)  
array([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])
>>> x2 = np.reshape(x, (2, 5))  
>>> x2  
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> np.roll(x2, 1)  
array([[9, 0, 1, 2, 3],
       [4, 5, 6, 7, 8]])
>>> np.roll(x2, -1)  
array([[1, 2, 3, 4, 5],
       [6, 7, 8, 9, 0]])
>>> np.roll(x2, 1, axis=0)  
array([[5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4]])
>>> np.roll(x2, -1, axis=0)  
array([[5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4]])
>>> np.roll(x2, 1, axis=1)  
array([[4, 0, 1, 2, 3],
       [9, 5, 6, 7, 8]])
>>> np.roll(x2, -1, axis=1)  
array([[1, 2, 3, 4, 0],
       [6, 7, 8, 9, 5]])
>>> np.roll(x2, (1, 1), axis=(1, 0))  
array([[9, 5, 6, 7, 8],
       [4, 0, 1, 2, 3]])
>>> np.roll(x2, (2, 1), axis=(1, 0))  
array([[8, 9, 5, 6, 7],
       [3, 4, 0, 1, 2]])

相關用法


注:本文由純淨天空篩選整理自dask.org大神的英文原創作品 dask.array.roll。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。