This counts how many numbers are greater than 3: 2 (which are 4 and 5).
NumPy
print(np.count_nonzero(arr > 3))
Sample Program
This program shows how aggregation helps summarize sales data. It finds the total sales, average sales per day, and the highest sales each store made on any day.
NumPy
import numpy as np
# Create a 2D array representing daily sales for 3 stores over 4 days
sales = np.array([
[10, 15, 20, 25],
[5, 10, 15, 20],
[8, 12, 18, 22]
])
# Total sales for all stores and days
total_sales = np.sum(sales)
# Average sales per day (across all stores)
avg_sales_per_day = np.mean(sales, axis=0)
# Maximum sales in a single day for each store
max_sales_per_store = np.max(sales, axis=1)
print(f"Total sales: {total_sales}")
print(f"Average sales per day: {avg_sales_per_day}")
print(f"Max sales per store: {max_sales_per_store}")
OutputSuccess
Important Notes
Aggregation reduces many numbers into a few meaningful values.
Using axis=0 aggregates down columns; axis=1 aggregates across rows.
Aggregation is useful for quick insights and comparisons.
Summary
Aggregation turns many data points into simple summaries.
Common aggregations include sum, mean, max, min, and count.
They help us understand data quickly and clearly.
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
Step 1: Understand aggregation meaning
Aggregation means combining many values into one summary value.
Step 2: Identify common aggregation uses
Common aggregations are sum, mean, max, min, which summarize data quickly.
Final Answer:
To summarize many data points into simple values like sums or averages -> Option C
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
Step 1: Recall numpy array methods
Numpy arrays have a method called mean() to calculate average.
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.
Final Answer:
arr.mean() -> Option A
Quick Check:
Array method for mean = arr.mean() [OK]
Hint: Use array.method() for aggregation in numpy [OK]