-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGMM_and_K_means.py
174 lines (143 loc) · 5.67 KB
/
GMM_and_K_means.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
############################################
#EM algorithm for a Guassian mixture model #
############################################
import numpy as np
import numpy.random as rnd
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal
def gaussian_mixture_model(data):
"""
EM algorithm for a Guassian mixture model with 2 gaussians on the given dataset, and returns the parameters for the model.
:param np.ndarray data: The data of size (n, k), where n is the number of points and k is the dimensionality of each point.
:rtype tuple: (pi_1, pi_2, mu_1, mu_2, sigma_1, sigma_2)
"""
n = data.shape[0] # number of points
k = data.shape[1] # the dimensionality of each point
num_iterations = 80
# initialize the priors to be equal
pi_1 = 0.5
pi_2 = 0.5
# get the initial means as random datapoints
initial_means = rnd.permutation(data)[:2]
mu_1 = initial_means[0, :]
mu_2 = initial_means[1, :]
# start with the covariance matrices as the identity matrix times a large constant
sigma_1 = np.identity(k) * 100
sigma_2 = np.identity(k) * 100
z = np.zeros((n, 2))
for iter in range(num_iterations):
# E-step
pdf_1 = multivariate_normal(mean=mu_1, cov=sigma_1)
pdf_2 = multivariate_normal(mean=mu_2, cov=sigma_2)
for i in range(n):
# prob_1 = pi_1 * pdf(data[i, :], mu_1, sigma_1)
prob_1 = pi_1 * pdf_1.pdf(data[i, :])
# prob_2 = pi_2 * pdf(data[i, :], mu_2, sigma_2)
prob_2 = pi_2 * pdf_2.pdf(data[i, :])
z[i, 0] = prob_1 / (prob_1 + prob_2)
z[i, 1] = prob_2 / (prob_1 + prob_2)
# M-step
# Update pi
sums = np.sum(z, axis=0)
pi_1 = sums[0] / n
pi_2 = sums[1] / n
# Update mu
new_mu_1 = np.zeros(k)
new_mu_2 = np.zeros(k)
for i in range(n):
new_mu_1 += z[i, 0] * data[i, :]
new_mu_2 += z[i, 1] * data[i, :]
mu_1 = new_mu_1 / sums[0]
mu_2 = new_mu_2 / sums[1]
# Update sigma
new_sigma_1 = np.zeros((k, k))
new_sigma_2 = np.zeros((k, k))
for i in range(n):
tmp = np.zeros((k, 1))
tmp[:, 0] = data[i, :] - mu_1
new_sigma_1 += z[i, 0] * np.dot(tmp, tmp.T)
tmp[:, 0] = data[i, :] - mu_2
new_sigma_2 += z[i, 1] * np.dot(tmp, tmp.T)
sigma_1 = new_sigma_1 / sums[0]
sigma_2 = new_sigma_2 / sums[1]
return pi_1, pi_2, mu_1, mu_2, sigma_1, sigma_2
def dist(x, centroid):
res = 0
for i in range(len(x)):
res += (x[i] - centroid[i])**2
return res
#####################################################################################################################
# Runs the K-means algorithm to find two clusters on the given dataset, and returns the centroids for each cluster. #
#####################################################################################################################
def kmeans(data):
"""
Runs the K-means algorithm
:param np.ndarray data: The data of size (n, k), where n is the number of points and k is the dimensionality of
each point.
:rtype tuple: (centroid_1, centroid_2)
"""
def dist(x, centroid):
res = 0
for i in range(len(x)):
res += (x[i] - centroid[i])**2
return res
n = data.shape[0] # number of points
k = data.shape[1]
num_iterations = 80
# get the initial means as random datapoints
initial_means = rnd.permutation(data)[:2]
centroid_1 = initial_means[0, :]
centroid_2 = initial_means[1, :]
z = np.zeros(n)
for iter in range(num_iterations):
# E-step
for i in range(n):
dist_1 = dist(data[i, :], centroid_1)
dist_2 = dist(data[i, :], centroid_2)
if dist_1 < dist_2:
z[i] = 0
else:
z[i] = 1
# M-step
new_centroid_1 = np.zeros(k)
cnt_1 = 0
new_centroid_2 = np.zeros(k)
cnt_2 = 0
for i in range(n):
if z[i] == 0:
new_centroid_1 += data[i, :]
cnt_1 += 1
else:
new_centroid_2 += data[i, :]
cnt_2 += 1
centroid_1 = new_centroid_1 / cnt_1
centroid_2 = new_centroid_2 / cnt_2
return centroid_1, centroid_2
# Run the GMM
pi_1, pi_2, mu_1, mu_2, sigma_1, sigma_2 = gaussian_mixture_model(input_data)
# Run K-means
centroid_1, centroid_2 = kmeans(input_data)
# Make the hard cluster assignments and plot the data
gmm_clusters = np.zeros(input_data.shape[0])
kmeans_clusters = np.zeros(input_data.shape[0])
pdf_1 = multivariate_normal(mean=mu_1, cov=sigma_1)
pdf_2 = multivariate_normal(mean=mu_2, cov=sigma_2)
for i in range(input_data.shape[0]):
# GMM
prob_1 = pi_1 * pdf_1.pdf(input_data[i, :])
prob_2 = pi_2 * pdf_2.pdf(input_data[i, :])
gmm_clusters[i] = 0 if prob_1 > prob_2 else 1
# K-means
dist_1 = dist(input_data[i, :], centroid_1)
dist_2 = dist(input_data[i, :], centroid_2)
kmeans_clusters[i] = 0 if dist_1 < dist_2 else 1
# Plot GMM
plt.figure(1)
plt.title('Cluster assignments using MoG')
colors = ['b' if cluster == 0 else 'r' for cluster in gmm_clusters]
plt.scatter(input_data[:, 0], input_data[:, 1], c=colors)
# Plot K-means
plt.figure(2)
plt.title('Cluster assignments using K-means')
colors = ['b' if cluster == 0 else 'r' for cluster in kmeans_clusters]
plt.scatter(input_data[:, 0], input_data[:, 1], c=colors)