np。将ND张量/阵列与一维数组连接
问题内容:
我有两个数组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.repeat
:
r = np.concatenate((a, b.reshape(1, 1, -1).repeat(a.shape[0], axis=0)), axis=1)
它的作用是,首先调整b
数组的形状以匹配的尺寸a
,然后根据a
的第一个轴根据需要重复其值多次:
b3D = b.reshape(1, 1, -1).repeat(a.shape[0], axis=0)
array([[[1, 2, 3]],
[[1, 2, 3]],
[[1, 2, 3]],
[[1, 2, 3]],
[[1, 2, 3]]])
b3D.shape
(5, 1, 3)
然后将此中间结果与a
-
r = np.concatenate((a, b3d), axis=0)
r.shape
(5, 5, 3)
这与您当前的答案有所不同,主要是因为值的重复不是硬编码的(即,重复处理了它)。
如果您需要针对其他数量的尺寸(而不是3D阵列)处理此问题,则需要进行一些更改(主要是删除的硬编码重塑形状b
)。
时机
a = np.random.randn(100, 99, 100)
b = np.random.randn(100)
# Tai's answer
%timeit np.insert(a, 4, b, axis=1)
100 loops, best of 3: 3.7 ms per loop
# Divakar's answer
%%timeit
b3D = np.broadcast_to(b,(a.shape[0],1,len(b)))
np.concatenate((a,b3D),axis=1)
100 loops, best of 3: 3.67 ms per loop
# solution in this post
%timeit np.concatenate((a, b.reshape(1, 1, -1).repeat(a.shape[0], axis=0)), axis=1)
100 loops, best of 3: 3.62 ms per loop
这些都是极具竞争力的解决方案。但是,请注意性能取决于实际数据,因此请确保先进行测试!