37. Centering Matrix#

import numpy as np
n = 10
v = np.random.randn(n)
v
array([ 0.68000931,  1.39763564,  1.29460954, -1.67176175,  0.77330001,
       -0.65511793,  1.56499832,  1.57064905, -0.0904953 ,  0.22965829])
v.mean()
0.5093485192366817
v_c = v - v.mean()
v_c
array([ 0.17066079,  0.88828712,  0.78526102, -2.18111027,  0.26395149,
       -1.16446645,  1.0556498 ,  1.06130053, -0.59984382, -0.27969023])
cm = np.identity(n) - np.ones((n,n))/n
cm
array([[ 0.9, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1],
       [-0.1,  0.9, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1],
       [-0.1, -0.1,  0.9, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1],
       [-0.1, -0.1, -0.1,  0.9, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1],
       [-0.1, -0.1, -0.1, -0.1,  0.9, -0.1, -0.1, -0.1, -0.1, -0.1],
       [-0.1, -0.1, -0.1, -0.1, -0.1,  0.9, -0.1, -0.1, -0.1, -0.1],
       [-0.1, -0.1, -0.1, -0.1, -0.1, -0.1,  0.9, -0.1, -0.1, -0.1],
       [-0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1,  0.9, -0.1, -0.1],
       [-0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1,  0.9, -0.1],
       [-0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1,  0.9]])
np.matmul(cm, v)
array([ 0.17066079,  0.88828712,  0.78526102, -2.18111027,  0.26395149,
       -1.16446645,  1.0556498 ,  1.06130053, -0.59984382, -0.27969023])
Created in deepnote.com Created in Deepnote