Column-wise Aggregation

~10 mincode completion

Implement column_sums(X) that returns the sum of each column.

Examples

2x3 matrix column sums

Input
column_sums([[1, 2, 3], [4, 5, 6]])
Output
[5, 7, 9]

3x2 matrix column sums

Input
column_sums([[10, 20], [30, 40], [50, 60]])
Output
[90, 120]

Cancelling values give zero

Input
column_sums([[1, 0], [-1, 0]])
Output
[0, 0]

Hints

Hint 1

The reduction runs down the columns, so pass .

Hint 2

Reach for 0 rather than axis 1.

Requirements

  • X: 2D array of shape (m, n)

  • Return 1D array of shape (n,) containing the sum of each column.

  • Use a fully vectorised implementation without Python loops

Constraints

  • Vectorised implementation only, no Python loops

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~10 min

8 employers weight this skill

3 quant funds, 2 big tech firms, 1 frontier lab, 1 autonomy company, 1 enterprise vendor. Top match scores 81.

Python
import numpy as np

def column_sums(X: np.ndarray) -> np.ndarray:
    """
    Return the sum of each column.

    Args:
        X: 2D array of shape (m, n)

    Returns:
        1D array of shape (n,) containing the sum of each column.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.