numpy从2d数组中减去/添加1d数组

时间:2020-11-27 21:19:45

I have the following 2D-array:

我有以下2D数组:

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

and another 1D-array:

和另一个1D阵列:

b = array([ 1,  2,  3,  4,  5])

then I want to calculate something like

然后我想计算类似的东西

c = a - b

with the intent of getting:

旨在获得:

c = array([[0, 1,  2],
           [2, 3,  4],
           [4, 5,  6],
           [6, 7,  8],
           [8, 9, 10]])

but instead I get the error message:

但我得到错误信息:

Traceback (most recent call last):
  Python Shell, prompt 79, line 1
ValueError: operands could not be broadcast together with shapes (5,3) (5,)

I read the broadcasting rules but didn´t get any wiser. I could do a workaround with for-loops or similar but there should be a direct way. Thanks

我阅读了广播规则,但没有更明智。我可以使用for循环或类似方法进行解决,但应该有一个直接的方法。谢谢

2 个解决方案

#1


15  

You need to convert array b to a (2, 1) shape array, use None or numpy.newaxis in the index tuple. Here is the Indexing of Numpy array.

您需要将数组b转换为(2,1)形状数组,在索引元组中使用None或numpy.newaxis。这是Numpy数组的索引。

You can do it Like:

你可以这样做:

import numpy

a = numpy.array([[ 1,  2,  3],
           [ 4,  5,  6],
           [ 7,  8,  9],
           [10, 11, 12],
           [13, 14, 15]])

b = numpy.array([ 1,  2,  3,  4,  5])
c=a - b[:,None]
print c

Output:

输出:

Out[2]: 
array([[ 0,  1,  2],
       [ 2,  3,  4],
       [ 4,  5,  6],
       [ 6,  7,  8],
       [ 8,  9, 10]])

#2


2  

As Divakar specified in the comments, just add a new axis to b.

正如评论中指定的Divakar,只需向b添加一个新轴。

I suggest you read more about broadcasting which is very often useful to vectorize computations in numpy: interestingly enough, a.transpose() - b wouldn't have raised an error (you'd need to transpose the result again to obtain your desired output).

我建议你阅读更多有关广播的内容,这对于在numpy中矢量化计算非常有用:有趣的是,a.transpose() - b不会引发错误(你需要再次转置结果以获得所需的输出)。

In this computaion, the first array's shape is (3, 5), and b.shape is (5,). So the shape of b corresponds to the tail of the shape of a, and broadcasting can happen. This is not the case when the shape of the first array is (5, 3), hence the error you obtained.

在这个计算中,第一个数组的形状是(3,5),b.shape是(5,)。因此b的形状对应于a的形状的尾部,并且可以发生广播。当第一个数组的形状为(5,3)时,情况并非如此,因此您获得的错误。

Here are some runtime tests to compare the speeds of the suggested answers, with your values for a and b : you can see that the differences are not really significant

以下是一些运行时测试,用于比较建议答案的速度,以及a和b的值:您可以看到差异不是很显着

In [9]: %timeit (a.T - b).T
Out[9]: 1000000 loops, best of 3: 1.32 µs per loop

In [10]: %timeit a - b[:,None]
Out[10]: 1000000 loops, best of 3: 1.25 µs per loop

In [11]: %timeit a - b[None].T
Out[11]: 1000000 loops, best of 3: 1.3 µs per loop

#1


15  

You need to convert array b to a (2, 1) shape array, use None or numpy.newaxis in the index tuple. Here is the Indexing of Numpy array.

您需要将数组b转换为(2,1)形状数组,在索引元组中使用None或numpy.newaxis。这是Numpy数组的索引。

You can do it Like:

你可以这样做:

import numpy

a = numpy.array([[ 1,  2,  3],
           [ 4,  5,  6],
           [ 7,  8,  9],
           [10, 11, 12],
           [13, 14, 15]])

b = numpy.array([ 1,  2,  3,  4,  5])
c=a - b[:,None]
print c

Output:

输出:

Out[2]: 
array([[ 0,  1,  2],
       [ 2,  3,  4],
       [ 4,  5,  6],
       [ 6,  7,  8],
       [ 8,  9, 10]])

#2


2  

As Divakar specified in the comments, just add a new axis to b.

正如评论中指定的Divakar,只需向b添加一个新轴。

I suggest you read more about broadcasting which is very often useful to vectorize computations in numpy: interestingly enough, a.transpose() - b wouldn't have raised an error (you'd need to transpose the result again to obtain your desired output).

我建议你阅读更多有关广播的内容,这对于在numpy中矢量化计算非常有用:有趣的是,a.transpose() - b不会引发错误(你需要再次转置结果以获得所需的输出)。

In this computaion, the first array's shape is (3, 5), and b.shape is (5,). So the shape of b corresponds to the tail of the shape of a, and broadcasting can happen. This is not the case when the shape of the first array is (5, 3), hence the error you obtained.

在这个计算中,第一个数组的形状是(3,5),b.shape是(5,)。因此b的形状对应于a的形状的尾部,并且可以发生广播。当第一个数组的形状为(5,3)时,情况并非如此,因此您获得的错误。

Here are some runtime tests to compare the speeds of the suggested answers, with your values for a and b : you can see that the differences are not really significant

以下是一些运行时测试,用于比较建议答案的速度,以及a和b的值:您可以看到差异不是很显着

In [9]: %timeit (a.T - b).T
Out[9]: 1000000 loops, best of 3: 1.32 µs per loop

In [10]: %timeit a - b[:,None]
Out[10]: 1000000 loops, best of 3: 1.25 µs per loop

In [11]: %timeit a - b[None].T
Out[11]: 1000000 loops, best of 3: 1.3 µs per loop