简体   繁体   English

如何在numpy中将(n,)数组添加到(n,m)数组?

[英]How to add (n,) array to (n,m) array in numpy?

Consider having two arrays:考虑有两个 arrays:
A with shape (n,)形状A (n,)
B with shape (n,m)形状为(n,m)B

How can I add those two together so that A[i] is added to all m values of B[i] ?如何将这两者加在一起,以便将A[i]添加到B[i]的所有 m 值?
Surely I could do it with for loops, but I want to learn how to properly use numpy.当然我可以用 for 循环来做,但我想学习如何正确使用 numpy。
So what's an elegant way to do it in numpy?那么在 numpy 中有什么优雅的方法呢?

Example for (n,) + (n,m) : (n,) + (n,m)的示例:

A = [1,2]
B = [[1,2,3],[4,5,6]]
A + B should be [[2,3,4],[6,7,8]]

It would also be interesting how I could add the other way (n,) + (m,n) :我如何添加另一种方式(n,) + (m,n)也很有趣:

A = [1,2,3]
B = [[1,2,3],[3,4,5]]
A + B should be [[2,4,6],[4,6,8]]

Edit:编辑:
The shapes I meant aren't the same as I wrote down.我指的形状与我写下的不一样。
The first example is not (n,1)+(n,m) and the second example is not (n,1)+(m,n) .第一个示例不是(n,1)+(n,m)并且第二个示例不是(n,1)+(m,n)
Before this edit I confused (n,) with (n,1) .在此编辑之前,我将(n,)(n,1)混淆了。 (and with (1,n) ) (和(1,n)

Numpy has a feature named broadcasting . Numpy 有一个名为broadcasting的功能。 See the docs查看文档

The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations.术语广播描述了 numpy 在算术运算时如何对待具有不同形状的 arrays。 Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes.在某些限制条件下,较小的阵列在较大的阵列上“广播”,以便它们具有兼容的形状。 Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. It does this without making needless copies of data and usually leads to efficient algorithm implementations.广播提供了一种矢量化数组操作的方法,因此循环发生在 C 而不是 Python 中。这样做不会产生不必要的数据副本,并且通常会导致高效的算法实现。 There are, however, cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows computation.然而,在某些情况下,广播不是一个好主意,因为它会导致 memory 的低效使用,从而减慢计算速度。

You can add this two arrays, you should just change dims of array A你可以添加这两个 arrays,你应该只改变数组 A 的 dims

A = np.array([1,2,3]).reshape(1,3)
B = np.array([[1,2,3],[3,4,5]])  # this is already 2 dimensional

print(A + B)

It would literally be just A + B , but your inputs aren't the arrays you think they are.它实际上只是A + B ,但您的输入不是您认为的 arrays 。

Your inputs are nested lists, and converted to arrays, your A examples are 1D, not 2D.您的输入是嵌套列表,并转换为 arrays,您的A示例是一维的,而不是二维的。 Your inputs should look like您的输入应该看起来像

A = numpy.array([[1],[2]])
B = numpy.array([[1,2,3],[4,5,6]])

Then A + B would work.然后A + B就可以了。

In [46]: A = [1,2]
    ...: B = [[1,2,3],[4,5,6]]
In [47]: A+B
Out[47]: [1, 2, [1, 2, 3], [4, 5, 6]]

Addition for lists is a join.列表的添加是一个连接。 But if you really mean you have arrays:但如果你真的是说你有 arrays:

In [48]: AA = np.array(A); BB = np.array(B)
In [49]: AA+BB
Traceback (most recent call last):
  File "<ipython-input-49-14674959eb4e>", line 1, in <module>
    AA+BB
ValueError: operands could not be broadcast together with shapes (2,) (2,3) 

But by making the first (2,1) shape, that does work:但是通过制作第一个 (2,1) 形状,这确实有效:

In [50]: AA[:,None]+BB
Out[50]: 
array([[2, 3, 4],
       [6, 7, 8]])

The 2nd case works without modification, adding a (3,) to (2,3).第二种情况无需修改即可工作,将 (3,) 添加到 (2,3)。 The first is automatically promoted to (1,3).第一个自动提升为 (1,3)。 Adding the lead dimension is automatic;添加引线尺寸是自动的; trailing dimensions are your responsibility:尾随维度是你的责任:

In [51]: A = np.array([1,2,3])
    ...: B = np.array([[1,2,3],[3,4,5]])
In [52]: A+B
Out[52]: 
array([[2, 4, 6],
       [4, 6, 8]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM