Suppose I have a matrix(df):
c1 c2 c3
c1 21.7 23.4 17.3
c2 23.4 25.8 17.0
c3 17.4 17.0 23.1
I want to normalize this matrix with this formula:
to produce this new normalized matrix:
c1 c2 c3
c1 1.0 0.97 0.63
c2 0.97 1.0 0.53
c3 0.63 0.53 1.0
where C'(1,2) = 23.4/(21.7+25.8-23.4)
= 0.97
I have tried using df.corrcoef
, but the results is not what I expect.
You may use broadcasting of numpy arrays over the diagonal of the array:
>>> arr = df.values
>>> arr
array([[ 21.7, 23.4, 17.3],
[ 23.4, 25.8, 17. ],
[ 17.4, 17. , 23.1]])
>>> arr / (np.diag(arr) + np.diag(arr)[:,np.newaxis] - arr)
array([[ 1. , 0.971 , 0.6291],
[ 0.971 , 1. , 0.5329],
[ 0.635 , 0.5329, 1. ]])
np.diag(arr) + np.diag(arr)[:,np.newaxis]
is equivalent to c(u,u) + c(v,v)
in your equation, for each pair of coordinates (u, v)
:
>>> np.diag(arr) + np.diag(arr)[:,np.newaxis]
array([[ 43.4, 47.5, 44.8],
[ 47.5, 51.6, 48.9],
[ 44.8, 48.9, 46.2]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.