Topics / 🤖 AI & Machine Learning / 🧮 Level 20: NumPy & Maths for ML

Python exercise · about 5 minutes · free

Broadcasting

Subtract each column's average from every row, in one line. Broadcasting stretches the smaller array to fit.

m.mean(axis=0) gives one mean per column (shape (4,)). m - that subtracts it from every row automatically: NumPy broadcasts the 1-D array across the rows.

Table. m (3 × 4): m1, m2, m3, m4; 10 | 20 | 30 | 40; 12 | 18 | 33 | 37; 14 | 22 | 27 | 43. Operation: m - m.mean(axis=0). Result centred: m1, m2, m3, m4; -2 | 0 | 0 | 0; 0 | -2 | 3 | -3; 2 | 2 | -3 | 3. column means [12, 20, 30, 40] stretched across every rowm (3 × 4)m1m2m3m4102030401218333714222743centredm1m2m3m4-20000-23-322-33m - m.mean(axis=0)column means [12, 20, 30, 40] stretched across every row
Your quest: Set centred to the score matrix with each match's (column's) average subtracted.
Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
Hintcentred = m - m.mean(axis=0)
Output appears here.

Open it in the full course → Free with Google: saves your progress and unlocks 627 lessons.

In this level you'll learn:
More in Level 20: NumPy & Maths for ML

Share on LinkedIn