📖 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 total sales and how sales accumulate over days.
🎯 Goal: Build a small program using NumPy to calculate the total sales using reduce and the running total using accumulate from the numpy universal functions (ufuncs).
📋 What You'll Learn
Create a NumPy array with daily sales numbers
Create a variable to hold the addition ufunc from numpy
Use the reduce method of the addition ufunc to find total sales
Use the accumulate method of the addition ufunc to find running totals
Print the total sales and running totals
💡 Why This Matters
🌍 Real World
Businesses often track daily or periodic data like sales, website visits, or sensor readings. Using ufunc methods like reduce and accumulate helps quickly summarize and analyze this data.
💼 Career
Data analysts and scientists use NumPy ufuncs to efficiently process large datasets and perform calculations like totals and running sums, which are common in reports and dashboards.
Progress0 / 4 steps
1
Create the daily sales data array
Create a NumPy array called daily_sales with these exact values: [10, 15, 7, 20, 13].
NumPy
Hint
Use np.array([...]) to create the array with the exact values.
2
Create the addition ufunc variable
Create a variable called add_ufunc and set it to the addition ufunc from NumPy using np.add.
NumPy
Hint
Use np.add to get the addition ufunc and assign it to add_ufunc.
3
Calculate total sales and running totals
Use the reduce method of add_ufunc on daily_sales to create a variable total_sales. Then use the accumulate method of add_ufunc on daily_sales to create a variable running_totals.
NumPy
Hint
Use add_ufunc.reduce(daily_sales) for total and add_ufunc.accumulate(daily_sales) for running totals.
4
Print the total sales and running totals
Print the total_sales variable. Then print the running_totals variable.
NumPy
Hint
Use print(total_sales) and print(running_totals) to show the results.
Practice
(1/5)
1. What does the reduce method of a NumPy ufunc do?
easy
A. It returns an array showing the intermediate results of the ufunc operation.
B. It combines all elements of an array into a single result using the ufunc operation.
C. It applies the ufunc element-wise without combining elements.
D. It sorts the array before applying the ufunc.
Solution
Step 1: Understand the purpose of reduce
The reduce method applies the ufunc cumulatively to the elements of the array, reducing it to a single value.
Step 2: Compare with other methods
Unlike accumulate, which returns intermediate results, reduce returns only the final combined result.
Final Answer:
It combines all elements of an array into a single result using the ufunc operation. -> Option B
Quick Check:
reduce = single combined result [OK]
Hint: Reduce gives one final result by combining all elements [OK]
Common Mistakes:
Confusing reduce with accumulate
Thinking reduce returns intermediate steps
Assuming reduce sorts the array
2. Which of the following is the correct syntax to use the accumulate method of the NumPy add ufunc on array a?
easy
A. np.add.accumulate(a)
B. np.accumulate.add(a)
C. a.accumulate(np.add)
D. np.add.reduce(a)
Solution
Step 1: Recall ufunc method syntax
NumPy ufuncs like add have methods such as accumulate accessed as np.add.accumulate(array).
Step 2: Check each option
np.add.accumulate(a) matches the correct syntax. Options B and C have wrong order or method calls. np.add.reduce(a) uses reduce, not accumulate.
Final Answer:
np.add.accumulate(a) -> Option A
Quick Check:
ufunc.method(array) = correct syntax [OK]
Hint: Use ufunc.method(array), e.g., np.add.accumulate(a) [OK]
Common Mistakes:
Swapping method and ufunc names
Calling accumulate on array instead of ufunc
Confusing reduce and accumulate syntax
3. What is the output of the following code?
import numpy as np
arr = np.array([1, 2, 3, 4])
result = np.multiply.accumulate(arr)
print(result)
medium
A. [1 3 6 10]
B. [1 2 3 4]
C. [24]
D. [1 2 6 24]
Solution
Step 1: Understand multiply.accumulate
This method returns intermediate cumulative products: first element, first*second, first*second*third, etc.
Step 2: Calculate step-by-step
1, 1*2=2, 2*3=6, 6*4=24, so output is [1, 2, 6, 24].
Final Answer:
[1 2 6 24] -> Option D
Quick Check:
Cumulative product = [1 2 6 24] [OK]
Hint: Accumulate shows stepwise results, here cumulative product [OK]
Common Mistakes:
Thinking accumulate returns only final product
Confusing accumulate with reduce
Miscomputing intermediate products
4. The following code throws an error. What is the cause?
import numpy as np
arr = np.array([1, 2, 3])
result = np.add.accumulate(arr, axis=1)
print(result)
medium
A. The add ufunc cannot be used with accumulate.
B. The accumulate method does not accept an axis argument.
C. The array is 1D, so axis=1 is invalid.
D. The array elements are not integers.
Solution
Step 1: Check array dimensions
The array arr is 1-dimensional with shape (3,).
Step 2: Understand axis parameter
Axis=1 refers to the second dimension, which does not exist in a 1D array, causing an error.
Final Answer:
The array is 1D, so axis=1 is invalid. -> Option C
Quick Check:
Axis must exist in array shape [OK]
Hint: Axis must be valid for array dimensions [OK]
Common Mistakes:
Assuming accumulate lacks axis parameter
Ignoring array shape when setting axis
Blaming ufunc or data type incorrectly
5. Given a 2D array arr = np.array([[1, 2], [3, 4]]), what is the output of np.add.reduce(arr, axis=0)?
hard
A. [4 6]
B. [1 2 3 4]
C. [10]
D. [1 5]
Solution
Step 1: Understand reduce with axis
Using reduce with axis=0 applies the ufunc cumulatively down each column.
Step 2: Calculate column-wise sums
First column: 1 + 3 = 4; second column: 2 + 4 = 6.
Final Answer:
[4 6] -> Option A
Quick Check:
Sum columns with reduce(axis=0) = [4 6] [OK]
Hint: Reduce with axis sums along that axis [OK]
Common Mistakes:
Confusing axis=0 with axis=1
Expecting flattened sum instead of column sums
Mixing accumulate and reduce outputs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.