Advertisement
Guest User

Untitled

a guest
Dec 25th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. def EM(X, k, w, m):
  2.     delta_max=0
  3.     sigma = np.random.rand(2,2,2)
  4.     g=1/2*np.ones((X.shape[0], k))
  5.     g0=np.random.rand(X.shape[0], k)
  6.     while True:
  7.         delta_max=0
  8.         for i in range(X.shape[0]):
  9.             for j in range(k):
  10.                 g0[i,j] = np.copy(g[i,j])
  11.                 g[i,j] = w[j]* fi(X[i], m[j], sigma[j])
  12.                 sum=0
  13.                 for s in range(k):
  14.                     sum += w[s]*fi(X[i], m[s], sigma[s])
  15.                 g[i,j]/=sum
  16.                 delta_max=np.maximum(delta_max, np.abs(g[i,j]-g0[i,j]))
  17.        
  18.         #print(delta_max)
  19.         for j in range(k):
  20.            
  21.             m[j] = 1/(X.shape[0]*w[j])#*np.sum(g[:,j].reshape(200,1)*X, axis=0) ##
  22.             L=0
  23.             H=0
  24.             for i in range(X.shape[0]):
  25.                 H+=g[i,j]*X[i]
  26.                 a= (X[i]-m[j])
  27.                 a=a.reshape(X.shape[1],1)
  28.                
  29.                 L+=g[i,j]*np.dot(a,a.transpose())
  30.                 #print(np.dot(a,a.transpose()))
  31.             #print(L)
  32.             sigma[j] = (1/(X.shape[0]*w[j]))*L
  33.             m[j]*=H
  34.             w[j]=np.mean(g[:,j])
  35.             #print (w[j])
  36.         #print(g[:,1])
  37.         print(delta_max)
  38.         if delta_max < 0.0001:
  39.             return (w, m, sigma)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement