两个矩阵合并成一个矩阵,并在中间增加一个维度

import numpy as np 

a = np.arange(1,7).reshape(2,3)
# a = [[1,2,3],[4,5,6]]
b = np.arange(7,13).reshape(2,3)
# b = [[7,8,9],[10,11,12]]

a = np.expand_dims(a,axis=1) # a.shape=(2,1,3)
b = np.expand_dims(b,axis=1) # b.shape=(2,1,3)

c = np.concatenate((a,b),axis=1) # c.shape=(2,2,3)
print(c)
# 输出为 [[[1,2,3]
#          [7,8,9]
#
#          [[4,5,6]
#           [10,11,12]]]

print(c[:,0,:])

#输出为 [[1,2,3]
#         [4,5,6]]

 

你可能感兴趣的:(python,numpy,机器学习)