GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — NumPy Broadcasting Exercises, Practice, Solution

  • Home
  • Python Home
  • Python Exercises
  • ▼Python NumPy
  • NumPy Home
  • ▼Basic Operations and Arrays
  • NumPy Basic
  • NumPy Array
  • ▼Mathematics, Linear Algebra, and Statistics
  • NumPy Mathematics
  • NumPy Linear Algebra
  • NumPy Statistics
  • ▼Random Numbers
  • NumPy Random
  • ▼Sorting, Searching, and Indexing
  • Sorting and Searching
  • NumPy Advanced Indexing
  • ▼Datetime and String Operations
  • NumPy DateTime
  • NumPy String
  • ▼Broadcasting and Memory Layout
  • NumPy Broadcasting
  • NumPy Memory Layout
  • ▼Performance Optimization and Interoperability
  • NumPy Performance Optimization
  • NumPy Interoperability
  • ▼Input/Output (I/O) Operations
  • NumPy I/O Operations
  • ▼Functions and Masked Arrays
  • NumPy Universal Functions
  • NumPy Masked Arrays
  • ▼Structured Arrays and SciPy Integration
  • NumPy Structured Arrays
  • NumPy Integration with SciPy
  • ▼Advanced Topics and Mastery
  • Advanced NumPy
  • Mastering Numpy
  • ..More to come..

NumPy Broadcasting Exercises, Practice, Solution

Last update on April 28 2025 13:26:39 (UTC/GMT +8 hours)


This resource offers a total of 100 NumPy Broadcasting problems for practice. It includes 20 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

The following exercises focus on NumPy's broadcasting capabilities, enabling efficient operations on arrays of different shapes without explicit looping. They cover element-wise arithmetic, reshaping for compatibility, and applying operations across different array dimensions.

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

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.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.