np.expand_dim用于给数组增加一维

np.expand_dim()用于给数组增加一维

import numpy as np

if __name__ == '__main__':
    # np.array 相乘
    a = np.array([[1,2],[3,4]])
    b = np.array([[5,6],[7,8]])
    print('orgin a')
    print(a)
    a = np.expand_dims(a, 0)
    print('expand a')
    print(a)

    print('orgin b')
    print(b)
    b = np.expand_dims(b, -1)
    print('expand b')
    print(b)

ans:

orgin a
[[1 2]
 [3 4]]
expand a
[[[1 2]
  [3 4]]]
orgin b
[[5 6]
 [7 8]]
expand b
[[[5]
  [6]]

 [[7]
  [8]]]
文章目录