w3resource

NumPy Broadcasting Exercises, Practice, Solution


This resource offers a total of 20 NumPy Broadcasting problems for practice.

[An Editor is available at the bottom of the page to write and execute the scripts.]

From numpy.org -

The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is "broadcast" across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. It does this without making needless copies of data and usually leads to efficient algorithm implementations. There are, however, cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows computation.


1. Element-wise Addition with Broadcasting

Write a NumPy program that performs element-wise addition using broadcasting.

Click me to see the sample solution


2. Row-wise Subtraction Using Broadcasting

Write a NumPy program that creates a 2D array x of shape (3, 4) and a 1D array y of shape (4,). Subtract y from each row of x using broadcasting.

Click me to see the sample solution


3. 3D Array Multiplication with 1D Array

Write a NumPy program that multiplies a 3D array of shape (2, 3, 4) by a 1D array of shape (4,) using broadcasting.

Click me to see the sample solution


4. Scalar Addition to a 2D Array

Write a NumPy program that adds a scalar value to a 2D array of shape (5, 5) using broadcasting.

Click me to see the sample solution


5. Reshape Arrays for Element-wise Multiplication

Given two arrays, x = np.array([1, 2, 3]) and y = np.array([4, 5, 6, 7, 8, 9]). Write a Numpy program that reshapes x and y to make them broadcast-compatible and perform element-wise multiplication.

Click me to see the sample solution


6. Column-wise Division Using Broadcasting

Write a NumPy program that divides each column of a 2D array x of shape (4, 3) by a 1D array y of shape (4,) using broadcasting.

Click me to see the sample solution


7. Transpose and Add 1D Array to Each Row

Given a 2D array of shape (3, 5) and a 1D array of shape (3,). Write a Numpy program that transposes the 2D array and add the 1D array to each row of the transposed array.

Click me to see the sample solution


8. Subtract 1D Array from 3D Array Slices

Write a NumPy program to subtract a 1D array y of shape (3,) from each 2D slice of a 3D array x of shape (2, 3, 4) using broadcasting.

Click me to see the sample solution


9. Reshape 1D Arrays for Cross Addition

Write a NumPy program that creates two 1D arrays x of shape (5,) and y of shape (5,). Reshape them to (5, 1) and (1, 5) respectively, and perform element-wise addition using broadcasting.

Click me to see the sample solution


10. Element-wise Division of 2D Array by 1D Array

Write a NumPy program that performs element-wise division of a 2D array x of shape (6, 4) by a 1D array y of shape (4,) using broadcasting.

Click me to see the sample solution


11. Add 3D Array and 2D Array Using Broadcasting

Given a 3D array x of shape (2, 3, 4) and a 2D array y of shape (3, 4). Write a NumPy program to add them using broadcasting.

Click me to see the sample solution


12. Scalar Multiplication on a 2D Array

Write a NumPy program that creates a 2D array x of shape (4, 4) and a scalar y. Perform element-wise multiplication of a and b using broadcasting.

Click me to see the sample solution


13. Multiply Each Row by 1D Array

Write a NumPy program that multiplies each row of a 2D array a of shape (4, 4) by a 1D array b of shape (4,) using broadcasting.

Click me to see the sample solution


14. Add 1D Array to Each 2D Slice of 3D Array

Write a Numpy program that adds a 1D array of shape (6,) to each 2D slice of a 3D array of shape (2, 6, 4) using broadcasting.

Click me to see the sample solution


15. Broadcast Addition for 3D and 1D Arrays

Given two arrays, x of shape (2, 1, 3) and y of shape (1, 4, 1), use broadcasting write a NumPy program to obtain a result of shape (2, 4, 3) through element-wise addition.

Click me to see the sample solution


16. Subtract 1D Array from 3D Array

Write a NumPy program to subtract a 1D array y of shape (4,) from a 3D array x of shape (2, 3, 4) using broadcasting.

Click me to see the sample solution


17. Divide Each Row of 2D Array by 1D Array

Create a 2D array x of shape (5, 5) and a 1D array y of shape (5,). Write a NumPy program that divides each row of a by b using broadcasting.

Click me to see the sample solution


18. Reshape 1D Arrays for 2D Addition

Given two 1D arrays a of shape (8,) and b of shape (4,), write a NumPy program to reshape them and perform element-wise addition using broadcasting to get a 2D array of shape (8, 4).

Click me to see the sample solution


19. Add 3D Array and 2D Array with Broadcasting

Write a NumPy program to create a 3D array x of shape (3, 1, 5) and a 2D array y of shape (3, 5). Add x and y using broadcasting.

Click me to see the sample solution


20. Multiply Each Column of 2D Array by 1D Array

Write a NumPy program that Multiplies each column of a 2D array x of shape (7, 3) by a 1D array y of shape (7,) using broadcasting.

Click me to see the sample solution


Python-Numpy Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.