Python
numpy

numpy.mean and keepdims option

X = np.random.rand(5, 10)
print(X)
# Recent versions of numpy
A = X.mean(axis=0)
B = X.mean(axis=1)
C = X.mean(axis=0, keepdims=True)
D = X.mean(axis=1, keepdims=True)
print("====keepdims False====")
print(A)
print(B)
print("====keepdims True====")
print(C)
print(D)
print("====X-====")
Y = X - D
print(Y)
xc = X - C
print(xc)

# Older versions of numpy
Z = X - X.mean(axis=1).reshape(-1, 1)
print("====Z====")
print(Z)
print(Z==Y)
print(Z==xc)
  • e.g.
[[ 0.25518457  0.3020462   0.82833829  0.34538812  0.21882783  0.74033878
   0.84537549  0.28770821  0.09940315  0.06847006]
 [ 0.27508833  0.45318817  0.46381574  0.07564968  0.55683363  0.5939703
   0.6426096   0.74924793  0.54931914  0.54007607]
 [ 0.0728746   0.44654499  0.82165271  0.5675067   0.86208338  0.89135909
   0.15331502  0.20468845  0.48690015  0.11149013]
 [ 0.24228404  0.87995429  0.38547148  0.81049191  0.67784326  0.5200897
   0.80242979  0.12312958  0.62984986  0.79826152]
 [ 0.38105088  0.03017003  0.55446269  0.35068574  0.39123161  0.2831094
   0.2009292   0.76581118  0.82980976  0.42681617]]
====keepdims False====
[ 0.24529648  0.42238074  0.61074818  0.42994443  0.54136394  0.60577345
  0.52893182  0.42611707  0.51905641  0.38902279]
[ 0.39910807  0.48997986  0.46184152  0.58698054  0.42140767]
====keepdims True====
[[ 0.24529648  0.42238074  0.61074818  0.42994443  0.54136394  0.60577345
   0.52893182  0.42611707  0.51905641  0.38902279]]
[[ 0.39910807]
 [ 0.48997986]
 [ 0.46184152]
 [ 0.58698054]
 [ 0.42140767]]
====X-====
[[-0.1439235  -0.09706187  0.42923022 -0.05371995 -0.18028024  0.34123071
   0.44626742 -0.11139986 -0.29970492 -0.33063801]
 [-0.21489153 -0.03679169 -0.02616412 -0.41433018  0.06685377  0.10399044
   0.15262974  0.25926807  0.05933928  0.05009622]
 [-0.38896693 -0.01529654  0.35981118  0.10566518  0.40024186  0.42951757
  -0.3085265  -0.25715307  0.02505863 -0.3503514 ]
 [-0.34469651  0.29297374 -0.20150906  0.22351136  0.09086272 -0.06689084
   0.21544925 -0.46385096  0.04286932  0.21128098]
 [-0.04035678 -0.39123763  0.13305502 -0.07072192 -0.03017606 -0.13829827
  -0.22047847  0.34440352  0.40840209  0.0054085 ]]
[[ 0.00988809 -0.12033453  0.2175901  -0.08455631 -0.32253611  0.13456533
   0.31644368 -0.13840886 -0.41965326 -0.32055273]
 [ 0.02979184  0.03080744 -0.14693244 -0.35429475  0.01546969 -0.01180316
   0.11367778  0.32313086  0.03026273  0.15105328]
 [-0.17242189  0.02416425  0.21090453  0.13756228  0.32071944  0.28558564
  -0.3756168  -0.22142862 -0.03215626 -0.27753266]
 [-0.00301245  0.45757355 -0.2252767   0.38054748  0.13647932 -0.08568375
   0.27349797 -0.30298749  0.11079345  0.40923873]
 [ 0.1357544  -0.3922107  -0.05628549 -0.07925869 -0.15013233 -0.32266406
  -0.32800262  0.33969411  0.31075335  0.03779338]]
====Z====
[[-0.1439235  -0.09706187  0.42923022 -0.05371995 -0.18028024  0.34123071
   0.44626742 -0.11139986 -0.29970492 -0.33063801]
 [-0.21489153 -0.03679169 -0.02616412 -0.41433018  0.06685377  0.10399044
   0.15262974  0.25926807  0.05933928  0.05009622]
 [-0.38896693 -0.01529654  0.35981118  0.10566518  0.40024186  0.42951757
  -0.3085265  -0.25715307  0.02505863 -0.3503514 ]
 [-0.34469651  0.29297374 -0.20150906  0.22351136  0.09086272 -0.06689084
   0.21544925 -0.46385096  0.04286932  0.21128098]
 [-0.04035678 -0.39123763  0.13305502 -0.07072192 -0.03017606 -0.13829827
  -0.22047847  0.34440352  0.40840209  0.0054085 ]]
[[ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]]
[[False False False False False False False False False False]
 [False False False False False False False False False False]
 [False False False False False False False False False False]
 [False False False False False False False False False False]
 [False False False False False False False False False False]]

Refs.