0
0
NumPydata~30 mins

Vectorization over loops in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Vectorization over loops
📖 Scenario: You work in a small bakery that tracks daily sales of three types of bread. You have the sales data for a week and want to calculate the total sales for each bread type.
🎯 Goal: Build a program that calculates total sales for each bread type using vectorized operations with numpy instead of loops.
📋 What You'll Learn
Create a numpy array with daily sales data for three bread types over 7 days.
Create a variable to hold the number of days.
Use vectorized numpy operations to calculate total sales for each bread type.
Print the total sales array.
💡 Why This Matters
🌍 Real World
Vectorized operations are used in data science to quickly process large datasets like sales, sensor data, or images without slow loops.
💼 Career
Data scientists and analysts use vectorization to write faster and cleaner code for data aggregation and analysis.
Progress0 / 4 steps
1
Create the sales data array
Create a numpy array called sales with the exact values: [[10, 15, 20], [12, 18, 22], [11, 14, 19], [13, 17, 21], [14, 16, 23], [15, 19, 20], [16, 20, 24]].
NumPy
Need a hint?

Use np.array([...]) with the exact nested list of sales values.

2
Create the days variable
Create a variable called days and set it to the number of rows in the sales array using sales.shape[0].
NumPy
Need a hint?

Use sales.shape[0] to get the number of rows (days).

3
Calculate total sales using vectorization
Create a variable called total_sales and set it to the sum of sales over the rows (axis 0) using np.sum(sales, axis=0).
NumPy
Need a hint?

Use np.sum with axis=0 to sum each bread type's sales over all days.

4
Print the total sales
Write a print statement to display the total_sales array.
NumPy
Need a hint?

Use print(total_sales) to display the result.