np.roll的用法

# -- coding: UTF-8 --
import numpy as np
# np.roll(a, shift, axis=None)
# 意思是将a,沿着axis的方向,滚动shift长度

# example1
a = np.arange(10)
print(a)
for i in range(5):
    a = np.roll(a, 2)
    print(a)


# example 2
b = np.reshape(a, (2, 5))
print(b)
for j in range(5):
    b = np.roll(b, shift=1, axis=1)
    print(b)

# example 3
c = np.arange(25)
c = np.reshape(c, (5, 5))
print(c)
for k in range(5):
    # 先横着翻,再竖着翻
    c = np.roll(np.roll(c, shift=1, axis=0), shift=1, axis=1)
    print(c)
阅读更多

没有更多推荐了,返回首页