【发布时间】:2018-01-02 19:25:21
【问题描述】:
我有两个数组 a & b
a.shape
(5, 4, 3)
array([[[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0.10772717, 0.604584 , 0.41664413]],
[[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0.10772717, 0.604584 , 0.41664413],
[ 0.95879616, 0.85575133, 0.46135877]],
[[ 0. , 0. , 0. ],
[ 0.10772717, 0.604584 , 0.41664413],
[ 0.95879616, 0.85575133, 0.46135877],
[ 0.70442301, 0.74126523, 0.88965603]],
[[ 0.10772717, 0.604584 , 0.41664413],
[ 0.95879616, 0.85575133, 0.46135877],
[ 0.70442301, 0.74126523, 0.88965603],
[ 0.8039435 , 0.62802183, 0.58885027]],
[[ 0.95879616, 0.85575133, 0.46135877],
[ 0.70442301, 0.74126523, 0.88965603],
[ 0.8039435 , 0.62802183, 0.58885027],
[ 0.95848603, 0.72429311, 0.71461332]]])
和b
array([ 0.79212707, 0.66629398, 0.58676553], dtype=float32)
b.shape
(3,)
我要获取数组
ab.shape
(5,5,3)
我做如下 首先
b = b.reshape(1,1,3)
然后
b=np.concatenate((b, b,b, b, b), axis = 0)
和
ab=np.concatenate((a, b), axis = 1)
ab.shape
(5, 5, 3)
我得到了正确的结果,但不是很方便,尤其是在步骤
b=np.concatenate((b, b,b, b, b), axis = 0)
当我必须多次输入时(真实的数据集有很多维度)。有没有更快的方法来得出这个结果?
【问题讨论】:
-
np.concatenate((a, b.reshape(1, 1, -1).repeat(a.shape[0], axis=0)), axis=1)?
标签: python arrays numpy concatenation