0
0
NumPydata~15 mins

Common broadcasting patterns in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Common broadcasting patterns
📖 Scenario: You work in a small bakery that tracks daily sales of different types of bread. You want to analyze the sales data to understand how many breads were sold each day and how sales compare to a daily target.
🎯 Goal: Build a small program using numpy arrays to apply common broadcasting patterns. You will create sales data, set a daily sales target, calculate total sales per day, and compare sales to the target using broadcasting.
📋 What You'll Learn
Use numpy arrays to store sales data
Create a daily sales target as a single number
Use broadcasting to sum sales per day
Use broadcasting to compare daily sales to the target
Print the final comparison result
💡 Why This Matters
🌍 Real World
Broadcasting is a powerful tool in data science to efficiently compare and combine data without writing loops. It helps analyze sales, sensor data, or any tabular data quickly.
💼 Career
Understanding broadcasting is essential for data analysts and scientists to write clean, fast, and readable code when working with arrays and matrices.
Progress0 / 4 steps
1
Create the sales data array
Import numpy as np and create a 2D numpy array called sales with these exact values: [[10, 15, 20], [12, 18, 25], [14, 20, 30]]. Each row is a day, and each column is a bread type.
NumPy
Need a hint?

Use np.array() to create the 2D array with the exact numbers.

2
Set the daily sales target
Create a variable called daily_target and set it to the integer 50. This is the sales goal for each day.
NumPy
Need a hint?

Just assign the number 50 to the variable daily_target.

3
Calculate total sales per day using broadcasting
Create a variable called total_sales that sums the sales of all bread types for each day using np.sum on sales with axis=1.
NumPy
Need a hint?

Use np.sum with axis=1 to sum rows (days).

4
Compare total sales to daily target and print result
Create a variable called target_met that compares total_sales to daily_target using the >= operator. Then print target_met.
NumPy
Need a hint?

Use total_sales >= daily_target to get a boolean array, then print it.