Command Palette

Search for a command to run...

Hectal
Phase 14: Working with Data: NumPy and pandas
Python · Exercise 2 of 6 PreviousNext
Beginner

Scale each column with broadcasting

Write scale_columns(m, factors). m is a 2-D array (a table of numbers) and factors is a 1-D array with one number per column. Return a new table where column 0 is multiplied by factors[0], column 1 by factors[1], and so on. Use no loop: broadcasting (Topic 14.3) does it.

Write centre_rows(m). For each row, work out that row's own mean (its average, Topic 14.4) and subtract it from every number in that row. Return the new table, again without a loop. Hint: keepdims=True.

What Check tests

  1. scale_columns scales every column by its own factor
  2. Neither function changes the array it was given
  3. scale_columns keeps the shape
  4. centre_rows makes each row's mean 0
  5. centre_rows gives the expected values
  6. Neither function uses a loop

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.