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
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,
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 ([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
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
result = int_a + int_b
print(result)
Output:
[[12 9 16]
[10 16 21]
[16 14 23]
[21 10 23]]
Get Our Python Developer Kit for Free
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.
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 [5,8,3]
in the
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 [5,8,3,2]
in the
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
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 [5,8,3,2]
, is reshaped into a (1,1,4) array. The shape of the
If you perform broadcasting between the
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]]]
Get Our Python Developer Kit for Free
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.