In this post, we will discuss ‘Broadcasting’ using NumPy. It is also used while implementing neural networks as these operations are memory and computationally efficient.

So let’s understand what Broadcasting means followed by a few examples!

Broadcasting describes the way numpy treats arrays with different shapes for arithmetic operations. The smaller array is broadcasted across the larger array so that they have compatible shapes. It provides a way to vectorize array operations thus leading to efficient implementations.

The light bordered boxes represent the broadcasted values, this extra memory is not actually allocated during the operation, but can be useful conceptually to imagine that it is.

In the above example for both cases output is same while the second case uses the concept of broadcasting and is more efficient. NumPy is elegant enough to use the original scalar value without actually making copies making broadcasting operations more efficient.

• Example 1- Arithmetic operation between two arrays where the first array has shape (m,n) and the second array with shape (m,1).

Output:

• Example 2- Arithmetic operation between two arrays where the first array has shape (m,n) and the second array with shape (1,n).

Output:

• Example 3- Arithmetic operation between an array and scalar value where the array has shape (1,n)

Output:

• Example 4- Arithmetic operation between an array and scalar value where the array has shape (m,1).

Output: