import numpy as np
X = np.array([[1., 2., 3.], [4., 5., 6.]])
print("column sum:", X.sum(axis=0).tolist())
print("row sum:", X.sum(axis=1).tolist())
centered = X - X.mean(axis=1, keepdims=True)
assert np.allclose(centered, [[-1, 0, 1], [-1, 0, 1]])
print("row centered:", centered.tolist())
features = np.array([[1., 10., 7.], [3., 20., 7.]])
mu = features.mean(axis=0, keepdims=True)
sd = features.std(axis=0, keepdims=True)
Z = (features - mu) / np.where(sd == 0, 1., sd)
assert np.allclose(Z, [[-1, -1, 0], [1, 1, 0]])
assert np.isfinite(Z).all()
print("standardized:", Z.tolist())
a = np.array([1., 3.]); b = np.array([10., 20., 30.])
outer = a[:, None] - b[None, :]
assert outer.shape == (2, 3)
print("pairwise difference:", outer.tolist())
try:
    X + np.array([1., 2.])
except ValueError:
    print("incompatible shapes correctly rejected")
else:
    raise AssertionError("broadcast must fail")
