Bird
Raised Fist0
NumPydata~30 mins

Why aggregation matters in NumPy - See It in Action

Choose your learning style10 modes available
Why aggregation matters
📖 Scenario: Imagine you work in a small grocery store. You have daily sales data for different fruits. You want to understand the total sales for each fruit to know which ones sell the most.
🎯 Goal: You will create a NumPy array with sales data, set a threshold for high sales, calculate total sales for each fruit, and finally print the fruits that have total sales above the threshold.
📋 What You'll Learn
Use NumPy to handle sales data
Create a 2D NumPy array with exact sales numbers
Set a threshold variable for high sales
Calculate total sales per fruit using aggregation
Print the fruits with total sales above the threshold
💡 Why This Matters
🌍 Real World
Stores and businesses use aggregation to understand total sales, helping them decide what products to stock more.
💼 Career
Data analysts and scientists often aggregate data to summarize and find important insights quickly.
Progress0 / 4 steps
1
Create sales data array
Create a NumPy array called sales with these exact daily sales numbers for fruits (rows are days, columns are fruits): [[10, 5, 8], [7, 3, 6], [12, 4, 9]]
NumPy
Hint

Use np.array() and pass the list of lists exactly as shown.

2
Set sales threshold
Create a variable called threshold and set it to 20 to represent high total sales.
NumPy
Hint

Just assign the number 20 to the variable threshold.

3
Calculate total sales per fruit
Use NumPy's aggregation to create a variable called total_sales that sums sales for each fruit (sum over rows). Use np.sum with axis=0.
NumPy
Hint

Use np.sum with axis=0 to add sales vertically for each fruit.

4
Print fruits with high total sales
Create a list called fruits with these exact names: ["Apples", "Bananas", "Oranges"]. Then print the names of fruits whose total sales in total_sales are greater than threshold. Use a for loop with variables fruit and sale iterating over zip(fruits, total_sales). Print each fruit if sale > threshold.
NumPy
Hint

Use a for loop with zip(fruits, total_sales) and print the fruit name if its sale is greater than threshold.

Practice

(1/5)
1. What is the main purpose of aggregation in data analysis using numpy?
easy
A. To remove missing values from data
B. To create new columns in a dataset
C. To summarize many data points into simple values like sums or averages
D. To sort data in ascending order

Solution

  1. Step 1: Understand aggregation meaning

    Aggregation means combining many values into one summary value.
  2. Step 2: Identify common aggregation uses

    Common aggregations are sum, mean, max, min, which summarize data quickly.
  3. Final Answer:

    To summarize many data points into simple values like sums or averages -> Option C
  4. Quick Check:

    Aggregation = summary values [OK]
Hint: Aggregation means summarizing data points simply [OK]
Common Mistakes:
  • Confusing aggregation with sorting
  • Thinking aggregation creates new columns
  • Mixing aggregation with data cleaning
2. Which of the following is the correct syntax to calculate the mean of a numpy array arr?
easy
A. arr.mean()
B. mean(arr)
C. numpy.mean(arr)
D. arr.average()

Solution

  1. Step 1: Recall numpy array methods

    Numpy arrays have a method called mean() to calculate average.
  2. Step 2: Check syntax correctness

    Calling arr.mean() is correct; mean(arr) is invalid without import, numpy.mean(arr) requires prefix, arr.average() does not exist.
  3. Final Answer:

    arr.mean() -> Option A
  4. Quick Check:

    Array method for mean = arr.mean() [OK]
Hint: Use array.method() for aggregation in numpy [OK]
Common Mistakes:
  • Using mean(arr) without numpy prefix or import
  • Using arr.average() which is not a numpy method
  • Confusing function and method syntax
3. What is the output of this code?
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr.sum())
medium
A. 5
B. 15
C. [1 2 3 4 5]
D. Error

Solution

  1. Step 1: Understand the array values

    The array contains numbers 1, 2, 3, 4, 5.
  2. Step 2: Calculate the sum of array elements

    Sum = 1+2+3+4+5 = 15.
  3. Final Answer:

    15 -> Option B
  4. Quick Check:

    Sum of 1 to 5 = 15 [OK]
Hint: Sum adds all elements in the array [OK]
Common Mistakes:
  • Confusing sum with max or count
  • Expecting array output instead of single value
  • Syntax errors from missing import
4. Identify the error in this code snippet:
import numpy as np
arr = np.array([1, 2, 3, 4])
result = arr.mean
print(result)
medium
A. mean() method does not exist in numpy
B. Using print instead of return
C. Array should be a list for mean to work
D. Missing parentheses when calling mean method

Solution

  1. Step 1: Check how mean is called

    In the code, arr.mean is assigned without parentheses, so it is a method reference, not a value.
  2. Step 2: Understand method call syntax

    To get the mean value, arr.mean() must be called with parentheses before assignment.
  3. Final Answer:

    Missing parentheses when calling mean method -> Option D
  4. Quick Check:

    Method call needs () [OK]
Hint: Call methods with () to get results [OK]
Common Mistakes:
  • Assigning method without calling it
  • Thinking print causes error
  • Believing mean only works on lists
5. You have a 2D numpy array representing sales data for 3 products over 4 days:
import numpy as np
sales = np.array([[10, 20, 30, 40],
                  [5, 15, 25, 35],
                  [2, 4, 6, 8]])

Which code correctly calculates the total sales per product?
hard
A. sales.sum(axis=1)
B. sales.sum(axis=0)
C. sales.mean(axis=1)
D. sales.max(axis=0)

Solution

  1. Step 1: Understand array shape and axis meaning

    The array shape is (3 products, 4 days). Axis 0 is down rows (products), axis 1 is across columns (days per product).
  2. Step 2: Choose axis to sum per product

    Summing along axis=1 adds sales across days for each product, giving total sales per product.
  3. Final Answer:

    sales.sum(axis=1) -> Option A
  4. Quick Check:

    Sum per product = sum across columns (axis=1) [OK]
Hint: Sum axis=1 sums rows (per product) in 2D array [OK]
Common Mistakes:
  • Using axis=0 sums per day, not per product
  • Using mean instead of sum for totals
  • Confusing axis numbering in numpy
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard