np.将一维ND张量/数组与一维数组连接 [英] np.concatenate a ND tensor/array with a 1D array

查看:96
本文介绍了np.将一维ND张量/数组与一维数组连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组& b

I have two arrays 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)

我得到了正确的结果,但这不是很方便,尤其是在步骤

I get the right result, but it's not very convenient especially at the step

b=np.concatenate((b, b,b, b, b), axis = 0)

当我不得不键入很多次时(实际数据集具有很多维度).有没有更快的方法来达到这个结果?

when I have to type many times (the real dataset has much dimensions). Are there any faster ways to come to this result?

推荐答案

您可以使用np.repeat:

r = np.concatenate((a, b.reshape(1, 1, -1).repeat(a.shape[0], axis=0)), axis=1)

它的作用是,首先调整您的b数组的形状以匹配a的尺寸,然后根据a的第一个轴将其值重复多次:

What this does, is first reshape your b array to match the dimensions of a, and then repeat its values as many times as needed according to a's first axis:

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)

这与您当前的答案不同,主要是因为值的重复不是硬编码的(即,重复由它来照顾).

This differs from your current answer mainly in the fact that the repetition of values is not hard-coded (i.e., it is taken care of by the repeat).

如果您需要针对不同数量的尺寸(不是3D数组)处理此问题,则需要进行一些更改(主要是如何删除b的硬编码重塑形状).

If you need to handle this for a different number of dimensions (not 3D arrays), then some changes are needed (mainly in how remove the hardcoded reshape of 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

这些都是极具竞争力的解决方案.但是,请注意,性能取决于您的实际数据,因此请确保先进行测试!

These are all pretty competitive solutions. However, note that performance depends on your actual data, so make sure you test things first!

这篇关于np.将一维ND张量/数组与一维数组连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆