# NumPy Broadcasting Exercises, Practice, Solution

## NumPy Broadcasting Exercises [ 20 exercises with solution]

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

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.** Write a NumPy program that performs element-wise addition using broadcasting.

Click me to see the sample solution

**2.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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.** 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

**Weekly Trends and Language Statistics**- Weekly Trends and Language Statistics