numpy数组 - 从A中有效地减去B的每一行

时间:2018-02-11 14:47:41

标签: python arrays numpy numpy-broadcasting

我有两个numpy数组a和b。我想从a中减去b的每一行。我试着用:

a1 - b1[:, None]

这适用于小型数组,但在实际数据大小方面需要太长时间。

a = np.arange(16).reshape(8,2)

a
Out[35]: 
array([[ 0,  1],
       [ 2,  3],
       [ 4,  5],
       [ 6,  7],
       [ 8,  9],
       [10, 11],
       [12, 13],
       [14, 15]])

b = np.arange(6).reshape(3,2)

b
Out[37]: 
array([[0, 1],
       [2, 3],
       [4, 5]])

a - b[:, None]
Out[38]: 
array([[[ 0,  0],
        [ 2,  2],
        [ 4,  4],
        [ 6,  6],
        [ 8,  8],
        [10, 10],
        [12, 12],
        [14, 14]],

       [[-2, -2],
        [ 0,  0],
        [ 2,  2],
        [ 4,  4],
        [ 6,  6],
        [ 8,  8],
        [10, 10],
        [12, 12]],

       [[-4, -4],
        [-2, -2],
        [ 0,  0],
        [ 2,  2],
        [ 4,  4],
        [ 6,  6],
        [ 8,  8],
        [10, 10]]])

%%timeit
a - b[:, None]
The slowest run took 10.36 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 3.18 µs per loop

对于较大的阵列,这种方法太慢/效率低。

a1 = np.arange(18900 * 41).reshape(18900, 41)

b1 = np.arange(2674 * 41).reshape(2674, 41)

%%timeit
a1 - b1[:, None]
1 loop, best of 3: 12.1 s per loop

%%timeit
for index in range(len(b1)):
    a1 - b1[index]
1 loop, best of 3: 2.35 s per loop

我可以使用任何numpy技巧加快速度吗?

1 个答案:

答案 0 :(得分:2)

您正在玩内存限制。

如果您的示例中有8位足以存储数据,请使用uint8:

import numpy as np
a1 = np.arange(18900 * 41,dtype=np.uint8).reshape(18900, 41)
b1 = np.arange(2674 * 41,dtype=np.uint8).reshape(2674, 41)
%time c1=(a1-b1[:,None])
#1.02 s