-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans.py
141 lines (104 loc) · 3.52 KB
/
kmeans.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
# https://deeplearningcourses.com/c/cluster-analysis-unsupervised-machine-learning-python
# https://www.udemy.com/cluster-analysis-unsupervised-machine-learning-python
from __future__ import print_function, division
from future.utils import iteritems
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics.pairwise import pairwise_distances
def d(u, v):
diff = u - v
return diff.dot(diff)
def cost(X, R, M):
cost = 0
for k in range(len(M)):
# method 1
# for n in range(len(X)):
# cost += R[n,k]*d(M[k], X[n])
# method 2
diff = X - M[k]
sq_distances = (diff * diff).sum(axis=1)
cost += (R[:,k] * sq_distances).sum()
return cost
def plot_k_means(X, K, max_iter=20, beta=3.0, show_plots=False):
N, D = X.shape
M = np.zeros((K, D))
# R = np.zeros((N, K))
exponents = np.empty((N, K))
# initialize M to random
for k in range(K):
M[k] = X[np.random.choice(N)]
costs = []
k = 0
for i in range(max_iter):
k += 1
# step 1: determine assignments / resposibilities
# is this inefficient?
for k in range(K):
for n in range(N):
exponents[n,k] = np.exp(-beta*d(M[k], X[n]))
R = exponents / exponents.sum(axis=1, keepdims=True)
print (R.sum(axis=0, keepdims=True))
# step 2: recalculate means
# decent vectorization
# for k in range(K):
# M[k] = R[:,k].dot(X) / R[:,k].sum()
# oldM = M
# full vectorization
M = R.T.dot(X) / R.sum(axis=0, keepdims=True).T
# print("diff M:", np.abs(M - oldM).sum())
c = cost(X, R, M)
costs.append(c)
if i > 0:
if np.abs(costs[-1] - costs[-2]) < 1e-5:
break
if len(costs) > 1:
if costs[-1] > costs[-2]:
pass
# print("cost increased!")
# print("M:", M)
# print("R.min:", R.min(), "R.max:", R.max())
if show_plots:
plt.plot(costs)
plt.title("Costs")
plt.show()
random_colors = np.random.random((K, 3))
colors = R.dot(random_colors)
plt.scatter(X[:,0], X[:,1], c=colors)
plt.show()
print("Cost", costs)
print("Means", M)
return M, R
def get_simple_data():
# assume 3 means
D = 2 # so we can visualize it more easily
s = 4 # separation so we can control how far apart the means are
mu1 = np.array([0, 0])
mu2 = np.array([s, s])
mu3 = np.array([0, s])
N = 9 # number of samples
X = np.zeros((N, D))
X[:3, :] = np.random.randn(3, D) + mu1
X[3:6, :] = np.random.randn(3, D) + mu2
X[6:, :] = np.random.randn(3, D) + mu3
return X
def main():
X = get_simple_data()
# what does it look like without clustering?
#plt.scatter(X[:,0], X[:,1])
#plt.show()
K = 3 # luckily, we already know this
plot_k_means(X, K, beta=1.0, show_plots=True)
#K = 3 # luckily, we already know this
# plot_k_means(X, K, beta=3.0, show_plots=True)
# K = 3 # luckily, we already know this
# plot_k_means(X, K, beta=10.0, show_plots=True)
# K = 5 # what happens if we choose a "bad" K?
# plot_k_means(X, K, max_iter=30, show_plots=True)
#
# K = 5 # what happens if we change beta?
# plot_k_means(X, K, max_iter=30, beta=0.3, show_plots=True)
if __name__ == '__main__':
main()