np.concatenate a ND tensor/array with a 1D array

前端 未结 4 1648
南笙
南笙 2021-01-14 09:33

I have two arrays a & b

a.shape
(5, 4, 3)
array([[[ 0.        ,  0.        ,  0.        ],
        [ 0.        ,  0.        ,  0.        ],
        [ 0.          


        
4条回答
  •  不要未来只要你来
    2021-01-14 09:59

    Here are some simple timings based on cᴏʟᴅsᴘᴇᴇᴅ's and Divakar's solutions:

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

    Output: The slowest run took 6.44 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: 3.68 µs per loop

    %timeit np.concatenate((a, np.broadcast_to(b[None,None], (a.shape[0], 1, len(b)))), axis=1)
    

    Output: The slowest run took 4.12 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: 10.7 µs per loop

    Now here is the timing based on your original code:

    %timeit original_func(a, b)
    

    Output: The slowest run took 4.62 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: 4.69 µs per loop

    Since the question asked for faster ways to come up with the same result, I would go for cᴏʟᴅsᴘᴇᴇᴅ's solution based on these problem calculations.

提交回复
热议问题