Aggregation Functions - NumPy Fundamentals
Implement Aggregation Functions in Python with NumPy. easy exercise with examples, hints, and instant feedback. No setup or signup required.
Aggregation Functions
What you'll build
A function compute_aggregations(arr) that summarises a 2D array globally, per row and per column, and locates its largest and smallest entries.
Background
Reductions are everywhere in ML: a loss is the mean over a batch, feature standardisation needs per-column means and standard deviations, and a classifier's prediction is the argmax over class scores in each row. The axis argument decides which dimension disappears.
Axis convention
For an array of shape (n_rows, n_cols):
- no
axis: reduce everything to one number axis=1: reduce across the columns, giving one value per row, shape(n_rows,)axis=0: reduce down the rows, giving one value per column, shape(n_cols,)
The standard deviation is the population version (NumPy's default, ddof=0):
std = sqrt( sum((x - mean)^2) / N ) N = total number of entries
Inputs
arr: 2D numeric array of shape(n_rows, n_cols)with at least one entry.
Expected Return Format
Return a dictionary with these keys:
- Global (scalars):
'global_mean','global_std'(population,ddof=0),'global_min','global_max','global_sum' - Row-wise (shape
(n_rows,)):'row_mean','row_sum' - Column-wise (shape
(n_cols,)):'col_mean','col_sum' - Indices:
'argmax'and'argmin', each a(row, col)tuple of integers
Edge cases and conventions
- Ties:
argmax/argminreturn the first occurrence in row-major order (row by row, left to right). arr.argmax()returns a flat index into the flattened array; convert it to(row, col)withnp.unravel_index(ordivmod(idx, n_cols)).
Constraints
Use NumPy reductions; no Python loops.
Follow-up
- When would you want
ddof=1, and what happens with it for an array of one element? - How do you get the argmax of each row, as used for classifier predictions?
Examples
Input: compute_aggregations(np.array([[1, 2, 3], [4, 5, 6]]))
Output: {'global_mean': 3.5, 'global_std': 1.707825127659933, 'global_min': 1, 'global_max': 6, 'global_sum': 21, 'row_mean': array([2., 5.]), 'row_sum': array([ 6, 15]), 'col_mean': array([2.5, 3.5, 4.5]), 'col_sum': array([5, 7, 9]), 'argmax': (1, 2), 'argmin': (0, 0)}The max (6) sits at row 1, column 2; std = sqrt(17.5 / 6) = 1.7078.
Input: compute_aggregations(np.array([[5, 1], [5, 0]]))["argmax"] Output: (0, 0)
Both 5s tie; the first one in row-major order wins.
Practice in Python
import numpy as np
def compute_aggregations(arr: np.ndarray) -> dict:
"""
Global, row-wise and column-wise statistics of a 2D array.
Args:
arr: 2D numeric array of shape (n_rows, n_cols)
Returns:
dict with keys:
'global_mean', 'global_std' (ddof=0), 'global_min',
'global_max', 'global_sum': scalars
'row_mean', 'row_sum': shape (n_rows,)
'col_mean', 'col_sum': shape (n_cols,)
'argmax', 'argmin': (row, col) tuples, first occurrence
"""
# Your code here
pass
The interactive editor loads with Python and NumPy. Run your implementation against 8 tests, or use the progressive hints.