PythiloLesson 2 of 4 · progress kept in this browser

Broadcasting and aggregates

Aggregates such as sum, mean, max and argmax collapse an array. The axis argument picks the direction that disappears.

import numpy as np

m = np.array([[1., 2.], [3., 4.]])
print(m.sum())        # 10.0
print(m.sum(axis=0))  # columns: [4. 6.]
print(m.sum(axis=1))  # rows: [3. 7.]
Your task

Write center_columns(m) subtracting each column's mean from that column. Write row_shares(m) dividing each row by its own sum, leaving all-zero rows as zeros. Write best_column(m) returning the index of the column with the largest total, as an int.

The first run loads NumPy into this browser.