Most of the operations in NumPy are performed element-by-element. With the help of an example, you can understand this concept. Suppose you want to add two NumPy arrays, X and Y. The figure below explains how NumPy perform the arithmetic operation between these two arrays. Actually, NumPy will add every ith element of X with the ith element of Y and produces the sum of X and Y.

Operations like this work fine when both arrays are of the same shape. The problem arises when arrays are of different shapes. To solve this issue, NumPy broadcasting comes into the picture.

Broadcasting comes into the scene when arithmetic operations are performed between arrays of unequal shape or size. In this, the elements of NumPy array whose dimension is less than other array is replicated in such a way that its dimension is matched with the larger array.

Following are the rules that NumPy follows when it goes with broadcasting:

- First of all, the rank or dimension of the two arrays is checked. If the rank is not same, then prepend 1 to the shape of the lower rank array to make its rank equal to the higher rank array and then move to step 2. If the rank of both the arrays is same, then go to step 2.
- Now the rank of the two arrays is equal. The size of axes in both the arrays must be either same or 1. If this condition satisfies, then both arrays are compatible for broadcasting.
- The size of the resulting array created by broadcasting is the maximum size along each dimension from the input arrays.

Let's see how broadcasting works between one-dimensional and two-dimensional NumPy array.

a =[[a11, a12, a13] [a21, a22, a23]] b = [b1, b2, b3]

The shape of array a is (2, 3), whereas the shape of array b is (3, ). 1 is prepended to the shape of array b, and its shape will become (1, 3). Now, the axes of the arrays are same or 1.

a 2x 3 b 1x 3 result 2x 3

Array b is transformed to the below form, and then broadcasting is applied.

b =[[b1, b2, b3] [b1, b2, b3]] a + b =[[a11 + b1, a12 + b2, a13 + b3] [a21 + b1, a22 + b2, a23 + b3]] a - b =[[a11 - b1, a12 - b2, a13 - b3] [a21 - b1, a22 - b2, a23 - b3]] a * b =[[a11 * b1, a12 * b2, a13 * b3] [a21 * b1, a22 * b2, a23 * b3]] a / b =[[a11 / b1, a12 / b2, a13 / b3] [a21 / b1, a22 / b2, a23 / b3]]

Let's see how broadcasting is performed between scalar and one-dimensional NumPy array.

a = [a1, a2, a3] b = b1

Array b will be transformed to `[b1, b1, b1]`

and then broadcasting is applied.

a + b = [a1 + b1, a2 + b1, a3 + b1] a - b = [a1 - b1, a2 - b1, a3 - b1] a * b = [a1 * b1, a2 * b1, a3 * b1] a / b = [a1 / b1, a2 / b1, a3 / b1]

Let's see how broadcasting is performed between scalar and two-dimensional NumPy array.

a =[[a11, a12, a13] [a21, a22, a23]] b = b1

Array b will be transformed to the below form and then broadcasting is applied.

b =[[b1, b1, b1] [b1, b1, b1]] a + b =[[a11 + b1, a12 + b1, a13 + b1] [a21 + b1, a22 + b1, a23 + b1]] a - b =[[a11 - b1, a12 - b1, a13 - b1] [a21 - b1, a22 - b1, a23 - b1]] a * b =[[a11 * b1, a12 * b1, a13 * b1] [a21 * b1, a22 * b1, a23 * b1]] a / b =[[a11 / b1, a12 / b1, a13 / b1] [a21 / b1, a22 / b1, a23 / b1]]

**Example 1**

A.shape = (2 x 3)

B.shape = (4 x 3)

The rows in both arrays are not same, so broadcasting is not possible.

**Example 2**

A.shape = (3 x 4)

B.shape = (3)

Array B will be shaped to (1 x 3) as per step 1 of broadcasting rules. But still, Numpy broadcasting is not possible because the columns are not same in both the arrays.

**Example 3**

A.shape = (2 x 3 x 4)

B.shape = (3 x 2)

Array B will be shaped to (1 x 3 x 2) as per step 1 of broadcasting rules. But still Numpy broadcasting is not possible because the number of elements in axis 2 in both the arrays is not same.

- How to convert Numpy Array to List?
- What is NumPy ndarray shape in Python?
- What is NumPy axis?
- What are NumPy data types?
- What are structured arrays in Python NumPy?
- What are NumPy array attributes?
- How to iterate over NumPy array?
- How to change data type of a given Numpy array?
- How to use NumPy split() in Python?
- How to use NumPy flatten() in Python?
- How to use NumPy ravel() in Python?
- What is the difference between NumPy flatten() and ravel()?