📖 Scenario: Imagine you have daily sales numbers for a small shop. You want to see how sales add up day by day to understand the total sales over time.
🎯 Goal: You will create a list of daily sales, then use np.cumsum() to find the running total of sales each day.
📋 What You'll Learn
Create a numpy array called daily_sales with exact values: 5, 10, 7, 3, 8
Create a variable called cumulative_sales that stores the cumulative sum of daily_sales using np.cumsum()
Print the cumulative_sales array
💡 Why This Matters
🌍 Real World
Cumulative sums are used in finance to track total earnings, in health to track total steps walked, and in sales to track total revenue over time.
💼 Career
Data analysts and scientists often use cumulative sums to understand trends and totals in datasets, making this a key skill for reporting and decision-making.
Progress0 / 4 steps
1
Create the daily sales data
Import numpy as np and create a numpy array called daily_sales with these exact values: 5, 10, 7, 3, 8
NumPy
Hint
Use np.array() to create the array with the exact numbers inside square brackets.
2
Calculate cumulative sales
Create a variable called cumulative_sales and set it to the cumulative sum of daily_sales using np.cumsum()
NumPy
Hint
Use np.cumsum() and pass daily_sales inside the parentheses.
3
Print the cumulative sales
Write a print statement to display the cumulative_sales array
NumPy
Hint
Use print(cumulative_sales) to show the running totals.
4
Explain the result
Add a print statement that explains the output by printing: 'Cumulative sales show total sales up to each day.'
NumPy
Hint
Use print() with the exact text inside quotes.
Practice
(1/5)
1. What does the np.cumsum() function do when applied to a 1D numpy array?
easy
A. Calculates the cumulative sum of the elements step by step
B. Calculates the product of all elements
C. Sorts the array in ascending order
D. Finds the maximum value in the array
Solution
Step 1: Understand the function purpose
np.cumsum() adds elements cumulatively, meaning each element is the sum of all previous elements plus itself.
Step 2: Compare with other options
Options B, C, and D describe different operations: product, sorting, and max value, which are not what np.cumsum() does.
Final Answer:
Calculates the cumulative sum of the elements step by step -> Option A
Quick Check:
np.cumsum() = cumulative sum [OK]
Hint: Remember: cumsum means running total sum [OK]
Common Mistakes:
Confusing cumulative sum with product
Thinking it sorts the array
Assuming it finds max or min values
2. Which of the following is the correct syntax to compute the cumulative sum of a numpy array arr?
easy
A. np.cumsum(arr)
B. All of the above
C. np.cumsum(arr, axis=0)
D. arr.cumsum()
Solution
Step 1: Check numpy function usage
np.cumsum(arr) is the basic syntax to get cumulative sum of all elements in the array.
Step 2: Check array method and axis parameter
arr.cumsum() is a valid method call on numpy arrays. Also, np.cumsum(arr, axis=0) computes cumulative sum along the first axis, valid for multi-dimensional arrays.
Final Answer:
np.cumsum(arr) -> Option A
Quick Check:
Basic syntax is np.cumsum(arr) [OK]
Hint: np and array method both work; axis optional [OK]
Common Mistakes:
Thinking only np.cumsum(arr) works
Ignoring axis parameter for multi-dimensional arrays