Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
X = np.ones((4, 3))
m = X.mean(axis=0)
print(m.shape, (X - m).shape)(3,) (4, 3)
- Expecting
(4,):axis=0collapses the rows, leaving one mean per column, somhas 3 values. - Expecting a
ValueErrorfor subtracting a(3,)array from a(4, 3)one: broadcasting matches the last axis and repeatsmfor every row.
- What does
X.mean(axis=1)return here, and why doesX - X.mean(axis=1)fail? - How does
keepdims=Truechange the shape of the mean and the subtraction?