numpy中的axis

numpy中的axis:

  • axis=0 代表是沿行的方向,无论是sum还是concatenate, 其它axis的维数是不变的
  • axis=1代表是沿列的方向
  • axis=None 代表是flatten之后的array
    在这里插入图片描述
>>> a1
array([[[5, 6, 1],
        [2, 9, 8]]])
>>> a1.shape
(1, 2, 3)
>>> np.sum(a1, axis=0)
array([[5, 6, 1],
       [2, 9, 8]])
>>> np.sum(a1, axis=1)
array([[ 7, 15,  9]])
>>> np.sum(a1, axis=2)
array([[12, 19]])

# 沿哪个axis来sum,哪个axis消失
>>> np.sum(a1, axis=0).shape
(2, 3)
>>> np.sum(a1, axis=1).shape
(1, 3)
>>> np.sum(a1, axis=2).shape
(1, 2)
>>> a2 = np.random.randint(1,10, size=(1,2,3))
>>> a2
array([[[7, 6, 1],
        [1, 2, 2]]])
>>> a1 = np.random.randint(1,10, size=(1,2,3))
>>> a1
array([[[5, 6, 1],
        [2, 9, 8]]])
>>> a2
array([[[7, 6, 1],
        [1, 2, 2]]])


>>> np.concatenate((a1,a2),axis=0)
array([[[5, 6, 1],
        [2, 9, 8]],

       [[7, 6, 1],
        [1, 2, 2]]])     

>>> np.concatenate((a1,a2),axis=1)
array([[[5, 6, 1],
        [2, 9, 8],
        [7, 6, 1],
        [1, 2, 2]]])
# 沿axis=2 concatenate后,维度为(1,2,6)
>>> np.concatenate((a1,a2),axis=2)
array([[[5, 6, 1, 7, 6, 1],
        [2, 9, 8, 1, 2, 2]]])
发布了62 篇原创文章 · 获赞 11 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/real_ilin/article/details/104653740