0
0
NumPydata~30 mins

Understanding ufunc methods (reduce, accumulate) in NumPy - Hands-On Activity

Choose your learning style9 modes available
Understanding ufunc methods (reduce, accumulate)
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(total_sales) and print(running_totals) to show the results.