See also
The term broadcasting describes how numpy treats arrays with differentshapes during arithmetic operations. Subject to certain constraints,the smaller array is “broadcast” across the larger array so that theyhave compatible shapes. Broadcasting provides a means of vectorizingarray operations so that looping occurs in C instead of Python. It doesthis without making needless copies of data and usually leads toefficient algorithm implementations. There are, however, cases wherebroadcasting is a bad idea because it leads to inefficient use of memorythat slows computation.
NumPy operations are usually done on pairs of arrays on anelement-by-element basis. In the simplest case, the two arrays musthave exactly the same shape, as in the following example:
>>>a=np.array([1.0,2.0,3.0])>>>b=np.array([2.0,2.0,2.0])>>>a*barray([ 2., 4., 6.])
NumPy’s broadcasting rule relaxes this constraint when the arrays’shapes meet certain constraints. The simplest broadcasting example occurswhen an array and a scalar value are combined in an operation:
>>>a=np.array([1.0,2.0,3.0])>>>b=2.0>>>a*barray([ 2., 4., 6.])
The result is equivalent to the previous example whereb was an array.We can think of the scalarb beingstretched during the arithmeticoperation into an array with the same shape asa. The new elements inb are simply copies of the original scalar. The stretching analogy isonly conceptual. NumPy is smart enough to use the original scalar valuewithout actually making copies, so that broadcasting operations are asmemory and computationally efficient as possible.
The code in the second example is more efficient than that in the firstbecause broadcasting moves less memory around during the multiplication(b is a scalar rather than an array).
When operating on two arrays, NumPy compares their shapes element-wise.It starts with the trailing dimensions, and works its way forward. Twodimensions are compatible when
If these conditions are not met, aValueError:framesarenotaligned exception is thrown, indicating thatthe arrays have incompatible shapes. The size of the resulting arrayis the maximum size along each dimension of the input arrays.
Arrays do not need to have the samenumber of dimensions. For example,if you have a256x256x3 array of RGB values, and you want to scaleeach color in the image by a different value, you can multiply the imageby a one-dimensional array with 3 values. Lining up the sizes of thetrailing axes of these arrays according to the broadcast rules, shows thatthey are compatible:
Image(3darray):256x256x3Scale(1darray):3Result(3darray):256x256x3
When either of the dimensions compared is one, the other isused. In other words, dimensions with size 1 are stretched or “copied”to match the other.
In the following example, both theA andB arrays have axes withlength one that are expanded to a larger size during the broadcastoperation:
A(4darray):8x1x6x1B(3darray):7x1x5Result(4darray):8x7x6x5
Here are some more examples:
A(2darray):5x4B(1darray):1Result(2darray):5x4A(2darray):5x4B(1darray):4Result(2darray):5x4A(3darray):15x3x5B(3darray):15x1x5Result(3darray):15x3x5A(3darray):15x3x5B(2darray):3x5Result(3darray):15x3x5A(3darray):15x3x5B(2darray):3x1Result(3darray):15x3x5
Here are examples of shapes that do not broadcast:
A(1darray):3B(1darray):4# trailing dimensions do not matchA(2darray):2x1B(3darray):8x4x3# second from last dimensions mismatched
An example of broadcasting in practice:
>>>x=np.arange(4)>>>xx=x.reshape(4,1)>>>y=np.ones(5)>>>z=np.ones((3,4))>>>x.shape(4,)>>>y.shape(5,)>>>x+y<type 'exceptions.ValueError'>: shape mismatch: objects cannot be broadcast to a single shape>>>xx.shape(4, 1)>>>y.shape(5,)>>>(xx+y).shape(4, 5)>>>xx+yarray([[ 1., 1., 1., 1., 1.], [ 2., 2., 2., 2., 2.], [ 3., 3., 3., 3., 3.], [ 4., 4., 4., 4., 4.]])>>>x.shape(4,)>>>z.shape(3, 4)>>>(x+z).shape(3, 4)>>>x+zarray([[ 1., 2., 3., 4.], [ 1., 2., 3., 4.], [ 1., 2., 3., 4.]])
Broadcasting provides a convenient way of taking the outer product (orany other outer operation) of two arrays. The following example shows anouter addition operation of two 1-d arrays:
>>>a=np.array([0.0,10.0,20.0,30.0])>>>b=np.array([1.0,2.0,3.0])>>>a[:,np.newaxis]+barray([[ 1., 2., 3.], [ 11., 12., 13.], [ 21., 22., 23.], [ 31., 32., 33.]])
Here thenewaxis index operator inserts a new axis intoa,making it a two-dimensional4x1 array. Combining the4x1 arraywithb, which has shape(3,), yields a4x3 array.
Seethis articlefor illustrations of broadcasting concepts.