0
0
NumPydata~15 mins

Broadcasting for outer products in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Broadcasting for Outer Products
📖 Scenario: Imagine you run a small bakery. You want to find out how many cookies you can make if you combine different numbers of dough balls with different baking trays. Each dough ball makes one cookie, and each tray can bake a certain number of cookies at once.
🎯 Goal: You will create two arrays: one for the number of dough balls and one for the number of trays. Then, you will use broadcasting to calculate the total number of cookies you can bake for every combination of dough balls and trays.
📋 What You'll Learn
Create a numpy array called dough_balls with values [2, 4, 6]
Create a numpy array called baking_trays with values [1, 3, 5]
Use broadcasting to calculate the outer product of dough_balls and baking_trays
Store the result in a variable called total_cookies
Print the total_cookies array
💡 Why This Matters
🌍 Real World
Bakers and food businesses often need to calculate production quantities for different ingredient and equipment combinations quickly.
💼 Career
Data scientists use broadcasting to perform efficient calculations on large datasets without writing slow loops.
Progress0 / 4 steps
1
Create the dough balls array
Create a numpy array called dough_balls with the exact values [2, 4, 6].
NumPy
Need a hint?

Use np.array to create the array with the given values.

2
Create the baking trays array
Create a numpy array called baking_trays with the exact values [1, 3, 5].
NumPy
Need a hint?

Use np.array to create the array with the given values.

3
Calculate the outer product using broadcasting
Use broadcasting to calculate the outer product of dough_balls and baking_trays. Store the result in a variable called total_cookies. Use the expression dough_balls[:, None] * baking_trays.
NumPy
Need a hint?

Use dough_balls[:, None] to reshape dough_balls for broadcasting.

4
Print the total cookies array
Print the total_cookies array to see the result.
NumPy
Need a hint?

Use print(total_cookies) to display the array.