Broadcasting is one of the most useful features of NumPy arrays in Python. With broadcasting you can perform different operations, like addition and multiplication, between NumPy arrays of different shapes. In this tutorial, we’ll introduce the concept of broadcasting and show you how to do it on your NumPy arrays.

As a general rule while broadcasting NumPy arrays, one of the arrays involved in the broadcasting operation must be a scalar or have 1 as one of its dimensions. Suppose you want to perform an operation between an array of shape (m, n) where m is number of rows and n is number of columns, and another array with shape (1, n). In a case like this, the column values for the array with shape (1, n) are replicated m times across the 1 dimension to match the shape of the array (m, n). This will become clearer with the help of these next few examples.

Broadcasting between a Scalar and a 1-Dimensional Array

When you perform a broadcasting operation between a scalar NumPy array of shape (1,) and a 1-dimensional array of shape (1, m), the scalar value is copied m times to create a 1-dimensional array. Let’s see an example.

The script below creates a scalar array, int_a, of shape (5,) and a 1-dimensional array of shape (1,5) named int_b. The shapes are printed in the output.

``````import numpy as np

int_a = np.array([5])
print(int_a.shape)

int_b = np.array([5,8,3,9,10])
int_b = int_b.reshape(1,5)
print(int_b.shape)``````

Output:

``````(1,)
(1, 5)``````

When you perform a broadcasting addition operation between the int_a and int_b arrays, the 5 in the scalar array is copied 5 times (the column dimension of the array int_b). This means that the final addition operation is performed between arrays `([5,5,5,5,5,])` and `([5,8,3,9,10])`. Look at the script below for reference.

``````result = int_a + int_b
print(result)``````

Output:

``[[10 13  8 14 15]]``

Broadcasting between a Scalar and a 2-Dimensional Array

When you perform a broadcasting operation between a scalar NumPy array of shape (1,) and a 2-dimensional array of shape (m, n), the scalar value is copied across m rows and n columns to create a 2-dimensional array.

Let’s see an example.

The script below creates a scalar array int_a of shape (1,) and a 2-dimensional array, int_b, of shape (4,3). Again, the shapes are printed in the output.

``````import numpy as np

int_a = np.array([5])
print(int_a.shape)

int_b = np.array([[ 7, 4, 11],
[ 5, 11, 16],
[11,  9, 18],
[16,  5, 18]]
)
print(int_b.shape)``````

Output:

``````(1,)
(4, 3)``````

Now if you perform any broadcasting operation, like adding the int_a and int_b arrays, the 5 in the scalar array is copied for 4 times across the rows (the row dimension of the array int_b) and 3 times across columns. The final addition operation is performed between two arrays of shapes (4,3). Look at the script below for reference.

``````result = int_a + int_b
print(result)``````

Output:

``````[[12  9 16]
[10 16 21]
[16 14 23]
[21 10 23]]``````

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit

Broadcasting between a 1-Dimensional Array and a 2-Dimensional Array

When you perform a broadcasting operation between a 1-dimensional array of shape (1,m) or (n,1) and a 2-dimensional array of shape (m, n), the array dimension which is not one in the 1-dimensional array is copied m times, or it’s copied n times to match the dimensions of the a 2-dimensional array with dimensions (m, n).

Here’s a good example. The script below creates a 1-dimensional array of shape (1,3) and a 2-dimensional array of shape (4,3).

``````import numpy as np

int_a = np.array([5,8,3])
int_a = int_a.reshape(1,3)
print(int_a.shape)

int_b = np.array([[ 7, 4, 11],
[ 5, 11, 16],
[11,  9, 18],
[16,  5, 18]]
)

print(int_b.shape)``````

Output:

``````(1, 3)
(4, 3)``````

Now, if you perform a broadcasting operation, such as addition between the int_a and int_b arrays that you defined in the previous script, the row `[5,8,3]` in the int_a array will be copied 4 times to match the dimension of the array int_b. The following script performs this broadcasting operation between arrays int_a and int_b.

``````result = int_a + int_b
print(result)``````

Output:

``````[[12 12 14]
[10 19 19]
[16 17 21]
[21 13 21]]``````

Let’s see another example of broadcasting between a 1-dimensional and a 2-dimensional array. This time, we’ll define a column array of shape (4,1) and a 2-dimensional array of shape (4,3)

``````import numpy as np

int_a = np.array([5,8,3,2])
int_a = int_a.reshape(4,1)
print(int_a.shape)

int_b = np.array([[ 7, 4, 11],
[ 5, 11, 16],
[11,  9, 18],
[16,  5, 18]]
)
print(int_b.shape)``````

Output:

``````(4, 1)
(4, 3)``````

Now, if you perform a broadcasting operation, like adding the int_a and int_b arrays, the column `[5,8,3,2]` in the int_a array will be copied 3 times to match the shape of the array int_b, i.e. (4,3). The script below performs the actual broadcasting operation between arrays int_a and int_b.

``````result = int_a + int_b
print(result)``````

Output:

``````[[12  9 16]
[13 19 24]
[14 12 21]
[18  7 20]]``````

You can see that the int_a array, with its 4 values, is added down each column of the int_b array instead of across each row since NumPy knew the int_b array also had 4 rows.

Broadcasting between a one-dimensional and a 3-dimesional array.

To perform broadcasting operations between a 1-dimensional and a 3-dimensional array, you have to add an instance dimension to the 1-dimensional array to make it 3-dimensional. For example, in the following script the int_a one dimensional array, `[5,8,3,2]`, is reshaped into a (1,1,4) array. The shape of the int_b array is (3,3,4).

If you perform broadcasting between the int_a and int_b arrays, the row with 4 items is copied 3 times to match each instance in the int_b array. Finally, the instance dimension in the int_a array is also copied 3 times to make the final shape as (3,3,4). The script below defines our int_a and int_b arrays, prints their original shapes, then performs our broadcasting operation and prints the final result.

``````import numpy as np

int_a = np.array([5,8,3,2])
int_a = int_a.reshape(1,1,4)

print("Original Shapes:")
print(int_a.shape)

int_b = np.array([
[[ 7, 4, 11, 25],
[ 5, 11, 16, 12],
[11,  9, 18, 16]],

[[ 5, 5, 15, 8],
[ 9, 6, 10, 6],
[15, 9, 13, 21]],

[[9, 7, 12, 12],
[24, 8, 9, 10],
[8, 21, 5, 12]]
]
)
print(int_b.shape)

print("\nOutput:")
result = int_a + int_b
print(result)``````

Output:

``````Original Shapes:
(1, 1, 4)
(3, 3, 4)

Output:
[[[12 12 14 27]
[10 19 19 14]
[16 17 21 18]]

[[10 13 18 10]
[14 14 13  8]
[20 17 16 23]]

[[14 15 15 14]
[29 16 12 12]
[13 29  8 14]]]``````