-
Notifications
You must be signed in to change notification settings - Fork 0
/
GMM_part2.py
57 lines (49 loc) · 1.65 KB
/
GMM_part2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#Import Libraries
import numpy as np
def GMM_part2 (X,K):
l = 10**20
while np.abs(l)>10**15:
n = X.shape[1]
d = X.shape[0]
TOL = 10**-10
Pi = np.random.normal(size = K)
Pi = np.exp(Pi)
Pi /= Pi.sum()
Pi = np.reshape(Pi,-1)
Mu = []
S = []
SS = []
# diag_S = []
inv_S = []
# diag_inv_S = []
Loss = []
for k in range(K):
mu = np.random.normal(size=(d, 1))
Mu.append(mu)
s = np.ones(1)
inv_s = 1/s
S.append(s)
SS.append(s*np.eye(d))
Max_iter = 500
r = np.zeros([n,K])
for iteration in range(Max_iter):
for k in range(K):
r[:,k] = Pi[k]*((S[k])**(-d/2))*(np.exp((-1/2)*np.sum((X-Mu[k]) * (X-Mu[k]) / S[k],axis=0)))
R1 = np.reshape(np.sum(r,axis=1),(n,1)) #ri.
r = r/R1
# r = r +10**-8
l = -np.log(R1).sum()
if np.abs(l)>10**15:
break
Loss.append(l)
if iteration > 1 and np.abs(Loss[iteration]-Loss[iteration-1])<=TOL*np.abs(Loss[iteration]):
break
R0 = np.reshape(np.sum(r,axis=0),(K)) #r.k
Pi = R0/n
for k in range(K):
Mu[k] = np.reshape(np.sum(r[:,k]*X,axis=1)/R0[k],(d,1))
S[k] = (np.sum(np.sum(r[:,k]*X**2,axis=1)/R0[k]-(Mu[k].T**2)))/d
SS[k] = S[k]*np.eye(d)
# print('Mu=',Mu)
# print('S=',SS)
return Mu,SS