a = np.arange(12).reshape(3,4)
a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
使用np.reshape()将a由二维扩展到三维.
b = np.reshape(a,(3,4,1))
b
array([[[ 0],
[ 1],
[ 2],
[ 3]],
[[ 4],
[ 5],
[ 6],
[ 7]],
[[ 8],
[ 9],
[10],
[11]]])
使用np.concatenate拼接,将(3,4,1)转成(3,4,3)
c = np.concatenate([b]*3, 2)
c
array([[[ 0, 0, 0],
[ 1, 1, 1],
[ 2, 2, 2],
[ 3, 3, 3]],
[[ 4, 4, 4],
[ 5, 5, 5],
[ 6, 6, 6],
[ 7, 7, 7]],
[[ 8, 8, 8],
[ 9, 9, 9],
[10, 10, 10],
[11, 11, 11]]])
c.shape
(3, 4, 3)