-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathem.py
More file actions
133 lines (108 loc) · 3.35 KB
/
Copy pathem.py
File metadata and controls
133 lines (108 loc) · 3.35 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#coding=utf-8
import numpy as np
import pylab
def normalize(X):
# num of column
ncol = X.shape[1]
# normalize
mu = np.mean(X, axis=0)
sig = np.std(X, axis=0)
for i in range(ncol):
X[:,i] = (X[:,i] - mu[i]) / sig[i]
return X
def gaussian(x, mu ,sigma):
Z = 1 / ( (2 * np.pi) ** (x.size / 2) )
Z *= 1 / (np.linalg.det(sigma) ** 0.5)
return Z * np.exp( -0.5 * np.dot( np.dot(x-mu, np.linalg.inv(sigma)), x-mu ) )
def likelihood(X, pi, mu, sigma):
N = len(X)
conf_p_X = np.zeros(N)
for n in range(N):
for k in range(K):
conf_p_X[n] += pi[k] * gaussian(X[n], mu[k], sigma[k])
return np.sum( np.log(conf_p_X) )
if __name__ == '__main__':
## setting
datapath = 'data/faithful.txt'
K = 2 # num of gauss elem.
N = 0 # num of data
D = 0 # dim of data
## load data and normalize it
X = np.genfromtxt(datapath)
N = len(X)
D = X.shape[1]
print 'data has %d dimentions' % D
X = normalize(X)
print X
## initialize
# mu: means
# sigma: cov-matrix
# pi: mixing coefficient
# gamma: burden rate
mu = np.random.rand(K, D)
print mu
sigma = range(K)
for k in range(K):
sigma[k] = np.identity(D)
print sigma
pi = np.random.rand(K)
print pi
gamma = np.zeros((N, K))
print gamma
## EM Algorithm
# llh: log( p(X|mu, pi, sigma) )
# step: upper bound of counter
llh = likelihood(X, pi, mu, sigma)
step = 20
counter = 0
while counter < step:
## E-step
for n in range(N):
# calc denominator (common)
denominator = 0.
for j in range(K):
denominator += pi[j] * gaussian(X[n], mu[j], sigma[j])
# calc buren rate for each k
for k in range(K):
gamma[n][k] = pi[k] * gaussian(X[n], mu[k], sigma[k]) / denominator
## M-step
for k in range(K):
# calc Nk
Nk = np.sum( gamma[:,k] )
# calc mu
print mu[k]
mu[k] = np.zeros(D)
for n in range(N):
mu[k] += gamma[n][k] * X[n]
mu[k] /= Nk
# calc sigma
sigma[k] = np.zeros((D, D))
for n in range(N):
diff = np.matrix( X[n] - mu[k] )
sigma[k] += gamma[n][k] * ( diff.T * diff )
sigma[k] /= Nk
# calc pi
pi[k] = Nk / N
# print condition
print "%d\t likelihood:%f" % (counter, llh)
llh = likelihood(X, pi, mu, sigma)
counter += 1
# draw means for each gauss dist.
for k in range(K):
pylab.scatter(*mu[k], color='r', marker='o')
# draw contour
S = np.linspace(-2.5, 2.5, 50)
T = np.linspace(-2.5, 2.5, 50)
s,t = np.meshgrid(S,T)
for k in range(K):
sigmax = np.sqrt( sigma[k][0,0] ) # std. devariation
sigmay = np.sqrt( sigma[k][1,1] ) # std. devariation
sigmaxy = sigma[k][0,1] # codevariation
z = pylab.bivariate_normal(s, t, sigmax, sigmay, mu[k][0], mu[k][1], sigmaxy)
cs = pylab.contour(s, t, z, 3, colors='k', linewidths=1)
# draw training data
pylab.scatter(X[:,0], X[:,1], color='g', marker='x')
# show graph
pylab.xlim(-2.5, 2.5)
pylab.ylim(-2.5, 2.5)
pylab.show()