Column-wise Aggregation

Easy
~10 min
code completion

Aggregation Along an Axis

NumPy's aggregation functions accept an axis argument to reduce along a specific dimension.

X = np.array([[1, 2, 3],
              [4, 5, 6]])

np.sum(X, axis=0)  # column sums: [5, 7, 9]
np.sum(X, axis=1)  # row sums:    [6, 15]
np.mean(X, axis=0) # column means: [2.5, 3.5, 4.5]

axis=0 collapses rows (operates down the columns).

axis=1 collapses columns (operates across the rows).

Your task:

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

Example Tests

2x3 matrix column sums

Input: {"X":[[1,2,3],[4,5,6]]}

Expected: [5,7,9]

3x2 matrix column sums

Input: {"X":[[10,20],[30,40],[50,60]]}

Expected: [90,120]

Cancelling values give zero

Input: {"X":[[1,0],[-1,0]]}

Expected: [0,0]

Sign in to solve this problem

You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.