Array Broadcasting Examples
Below are some examples of NumPy array broadcasting in Python:
Array Broadcasting of Single-Dimensional Array
In this example, the code uses NumPy to create a 1×3 array ‘a’ with elements [17, 11, 19]. It then defines a scalar ‘b’ with a value of 3. Broadcasting is employed when adding ‘a’ and ‘b’ (a + b), performing element-wise addition, resulting in a new array ‘c’ with elements [20, 14, 22].
Python3
import numpy as np a = np.array([ 17 , 11 , 19 ]) # 1x3 Dimension array print (a) b = 3 print (b) # Broadcasting happened because of # miss match in array Dimension. c = a + b print (c) |
Output:
[17 11 19] 3 [20 14 22]
Array Broadcasting of Two-Dimensional Array
In this example, we are creating a 2×3 NumPy array ‘A’ and printing it. We then define a scalar ‘b’ with the value 4 and print it. Finally, we add ‘b’ to each element of ‘A’ to create a new array ‘C’ and print it.
Example
Python3
import numpy as np A = np.array([[ 11 , 22 , 33 ], [ 10 , 20 , 30 ]]) print (A) b = 4 print (b) C = A + b print (C) |
Output:
[[11 22 33] [10 20 30]] 4 [[15 26 37] [14 24 34]]
NumPy Broadcasting Operations
In this example, the code showcases NumPy broadcasting operations:
- Computing the outer product of vectors
- Broadcasting a vector to a matrix,
- Broadcasting a vector to the transposed matrix,
- Reshaping and broadcasting a vector to a matrix, and
- Performing scalar multiplication on a matrix.
Python3
import numpy as np v = np.array([ 1 , 2 , 3 ]) w = np.array([ 4 , 5 ]) # Outer product of vectors v and w print (np.reshape(v, ( 3 , 1 )) * w) x = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]]) # Broadcasting vector v to matrix x print (x + v) # Broadcasting vector w to the transposed matrix x print ((x.T + w).T) # Reshaping vector w and broadcasting to matrix x print (x + np.reshape(w, ( 2 , 1 ))) # Broadcasting scalar multiplication to matrix x print (x * 2 ) |
Output:
[[ 4 5] [ 8 10] [12 15]] [[2 4 6] [5 7 9]] [[ 5 6 7] [ 9 10 11]] [[ 5 6 7] [ 9 10 11]] [[ 2 4 6] [ 8 10 12]]
Broadcasting for Plotting a Two-Dimensional Function
Broadcasting is also frequently used in displaying images based on two-dimensional functions. If we want to define a function z=f(x, y).
In this example, we are utilizing NumPy and Matplotlib to generate and plot the sine and cosine curves. It first creates arrays for x coordinates ranging from 0 to 3π with a step of 0.1. Then, it computes the corresponding y values for sine and cosine functions.
Python3
import numpy as np import matplotlib.pyplot as plt # Computes x and y coordinates for # points on sine and cosine curves x = np.arange( 0 , 3 * np.pi, 0.1 ) y_sin = np.sin(x) y_cos = np.cos(x) # Plot the points using matplotlib plt.plot(x, y_sin) plt.plot(x, y_cos) plt.xlabel( 'x axis label' ) plt.ylabel( 'y axis label' ) plt.title( 'Sine and Cosine' ) plt.legend([ 'Sine' , 'Cosine' ]) plt.show() |
Output:
NumPy Array Broadcasting
The term broadcasting refers to the ability of NumPy to treat arrays with different dimensions during arithmetic operations. This process involves certain rules that allow the smaller array to be ‘broadcast’ across the larger one, ensuring that they have compatible shapes for these operations.
Broadcasting is not limited to two arrays; it can be applied over multiple arrays as well.
In this article, we will learn about broadcast over multiple arrays in the NumPy extension in Python.
Contact Us