numpy中矩阵的翻转(flip)

如何对矩阵进行翻转
首先,我们先随机生成一个3X3的矩阵
用法

numpy.flip(m, axis=None)[source]

Reverse the order of elements in an array along the given axis.
The shape of the array is preserved, but the elements are reordered.

import  numpy as np
a=np.random.randint(1,9,size=9).reshape((3,3))

[[5 8 6]
 [3 1 7]
 [8 7 8]]

如果上下翻转,则意味着行要发生翻转,自然axis=0

print(np.flip(a,axis=0))
[[8 7 8]
 [3 1 7]
 [5 8 6]]

如果左右翻转,则意为着列要发生翻转,自然axis=1

print(np.flip(a,axis=1))
[[6 8 5]
 [7 1 3]
 [8 7 8]]

你可能感兴趣的:(numpy)