Advertisement
Guest User

Untitled

a guest
Dec 10th, 2013
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  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.     H = -sum(c_normalized* np.log2(c_normalized))  
  19.     return H
  20.  
  21. A = np.array([ [ 2.0, 140.0, 128.23, -150.5, -5.4 ],
  22.             [ 2.4, 153.11, 130.34, -130.1,-9.5],
  23.             [ 1.2, 156.9, 120.11, -110.45,-1.12 ] ])
  24.  
  25. bins = 5 # ?
  26. n = A.shape[1]
  27. matMI = np.zeros((n, n))
  28.  
  29. for ix in np.arange(n):
  30.     for jx in np.arange(ix+1,n):
  31.         matMI[ix,jx] = calc_MI(A[:,ix], A[:,jx], bins)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement