Advertisement
Guest User

mod_minimal_MI_code

a guest
Dec 11th, 2013
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2.  
  3. def calc_MI(X,Y,bins):
  4.  
  5.    c_XY = np.histogram2d(X,Y,bins)[0]
  6.    c_X = np.histogram(X,bins)[0]
  7.    c_Y = np.histogram(Y,bins)[0]
  8.  
  9.    H_X = shan_entropy(c_X)
  10.    H_Y = shan_entropy(c_Y)
  11.    H_XY = shan_entropy(c_XY)
  12.  
  13.    MI = H_X + H_Y - H_XY
  14.    return MI
  15.  
  16. def shan_entropy(c):
  17.     c_normalized = c/float(np.sum(c))
  18.     c_normalized = c[np.nonzero(c_normalized)]
  19.     H = -sum(c_normalized* np.log2(c_normalized))  
  20.     return H
  21.  
  22. A = np.array([ [ 2.0, 140.0, 128.23, -150.5, -5.4 ],
  23.             [ 2.4, 153.11, 130.34, -130.1,-9.5],
  24.             [ 1.2, 156.9, 120.11, -110.45,-1.12 ] ])
  25.  
  26. bins = 5 # ?
  27. n = A.shape[1]
  28. matMI = np.zeros((n, n))
  29.  
  30. for ix in np.arange(n):
  31.     for jx in np.arange(ix+1,n):
  32.         matMI[ix,jx] = calc_MI(A[:,ix], A[:,jx], bins)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement