给定一个一维numpy数组,反转数组的三种方法:
- 切片索引
- numpy.flipud() 函数
- numpy.flip() 函数
import numpy as np
a = np.array([1,2,3,4,5])
# 切片索引
ans1 = a[::-1]
print(ans1) #[5 4 3 2 1]
# numpy.flipud() 函数
ans2 = np.flipud(a)
print(ans2) # [5 4 3 2 1]
# numpy.flip() 函数,可以实现矩阵反转,沿轴的方向反转,一维不需要指定
ans3 = np.flip(a)
print(ans3) # [5 4 3 2 1]
# 多维数组使用flip()
b = np.array([[5, 8, 6], [3, 1, 7], [8, 7, 8]])
print(b)
# [[5 8 6]
# [3 1 7]
# [8 7 8]]
reverse1 = np.flip(b, axis = 0) # 行内不变,列反转
print(reverse1)
# [[8 7 8]
# [3 1 7]
# [5 8 6]]
reverse2 = np.flip(b, axis = 1) # 列内不变,行反转
print(reverse2)
# [[6 8 5]
# [7 1 3]
# [8 7 8]]
reverse3 = np.flipud(b) # flipud()函数无axis参数,多维矩阵上的效果与flip(axis=0)相同
print(reverse3)
# [[8 7 8]
# [3 1 7]
# [5 8 6]]
标签:flipud,Python,反转,flip,print,模块,np,numpy
From: https://www.cnblogs.com/camilia/p/17451800.html