Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f4d1ff

Browse files
authored
Update em_mog.py
1 parent 81ec7b2 commit 3f4d1ff

File tree

1 file changed

+10
-43
lines changed

1 file changed

+10
-43
lines changed

em_mog.py

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ def em_mog(X, k, max_iter=20):
2828
ll_prev = float('inf')
2929
start = time.time()
3030

31-
#######################################################################
32-
# TODO: #
33-
# Initialize the means of the gaussians. You can use K-means! #
34-
#######################################################################
31+
#Perform KMeans to get the centers of the clusters
3532
kmeans = KMeans(n_clusters=k, random_state=0, max_iter=20).fit(X)
3633
mu = kmeans.cluster_centers_
3734

@@ -57,11 +54,8 @@ def em_mog(X, k, max_iter=20):
5754
# Computes final assignment
5855
w = e_step(X, mu, sigma, phi)
5956

60-
return phi, mu, sigma, w
57+
return phi, mu, sigma, w
6158

62-
#######################################################################
63-
# END OF YOUR CODE #
64-
#######################################################################
6559

6660
def log_likelihood(X, mu, sigma, phi):
6761
"""
@@ -71,19 +65,12 @@ def log_likelihood(X, mu, sigma, phi):
7165
ll = 0.0
7266
n, p = X.shape
7367
k = len(phi)
74-
#######################################################################
75-
# TODO: #
76-
# Compute the log-likelihood of the data under the current model. #
77-
# This is used to check for convergnence of the algorithm. #
78-
#######################################################################
68+
7969
for i in range(n):
8070
temp = 0
8171
for j in range(k):
8272
temp += phi[j] * mvn(mu[j].T, sigma[j]).pdf(X[i])
83-
ll += np.log(temp)
84-
#######################################################################
85-
# END OF YOUR CODE #
86-
#######################################################################
73+
ll += np.log(temp)
8774

8875
return ll
8976

@@ -95,38 +82,23 @@ def e_step(X, mu, sigma, phi):
9582
Returns:
9683
w: A vector of probabilities p(z==j|x; mu, sigma, phi) for the k
9784
gaussians per example of shape [n, k]
98-
"""
99-
#######################################################################
100-
# TODO: #
101-
# Perform the E-step of the EM algorithm. #
102-
# Use scipy.stats.multivariate_normal.pdf(...) to compute the pdf of #
103-
# of a gaussian with the current parameters. #
104-
#######################################################################
85+
"""
10586
n, p = X.shape
10687
k=len(phi)
10788
w = np.zeros((k, n))
10889
np.zeros((k, n))
10990
for j in range(k):
11091
for i in range(n):
11192
w[j, i] = phi[j] * mvn(mu[j], sigma[j]).pdf(X[i]) #No Error
112-
w /= w.sum(0)
113-
#######################################################################
114-
# END OF YOUR CODE #
115-
#######################################################################
93+
w /= w.sum(0)
11694

11795
return w
11896

11997

12098
def m_step(w, X, mu, sigma, phi, k):
12199
"""
122-
Computes the M-step of the EM algorithm.
123-
124-
"""
125-
#######################################################################
126-
# TODO: #
127-
# Update all the model parameters as per the M-step of the EM #
128-
# algorithm.
129-
#######################################################################
100+
Computes the M-step of the EM algorithm.
101+
"""
130102
n, p = X.shape
131103
phi = np.zeros(k)
132104
for j in range(len(mu)):
@@ -145,11 +117,6 @@ def m_step(w, X, mu, sigma, phi, k):
145117
for i in range(n):
146118
ys = np.reshape(X[i]- mu[j], (2,1)) #No error
147119
sigma[j] += w[j, i] * np.dot(ys, ys.T)
148-
sigma[j] /= w[j,:].sum()
149-
#######################################################################
150-
# END OF YOUR CODE #
151-
#######################################################################
120+
sigma[j] /= w[j,:].sum()
152121

153-
return phi, mu, sigma
154-
155-
122+
return phi, mu, sigma

0 commit comments

Comments
 (0)