forked from rhasan/machine-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk_mediods.py
174 lines (139 loc) · 6.06 KB
/
k_mediods.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
import numpy as np
import pylab as pl
import jellyfish
def initial_random_centers(X,K):
randidx = np.random.permutation(range(np.size(X,0)))
centers = X[randidx[0:K], :]
return (centers,randidx[0:K])
def find_closest_centers(X,center_idxs,distance_matrix):
K = np.size(center_idxs,0)
m = np.size(X,0)
idx = np.zeros(m,dtype=int)
for i in xrange(m):
min_d = np.inf
min_j = -1
for j in center_idxs:
d = distance_matrix[i,j]
if(min_d > d):
min_d = d
min_j = j
idx[i] = min_j
return idx
def compute_centers(X, idx, center_idxs,distance_matrix):
K = np.size(center_idxs,0)
moved_centers = np.zeros(K,dtype=int)
i = 0
for k in center_idxs:
(x_indxs,) = np.where(idx[:]==k)
#print "center:", k,"=",x_indxs
#print "vals", X[x_indxs,:]
min_cost = np.inf
min_c = -1
for c_indx in x_indxs:
cost = 0.0
for y_indx in x_indxs:
cost += distance_matrix[c_indx,y_indx]
#print "c_indx:", c_indx, "y_indx:",y_indx
#print "dist:", distance_matrix[c_indx,y_indx]
#print "cost:", cost
if min_cost > cost:
min_cost = cost
min_c = c_indx
moved_centers[i] = min_c
i += 1
#print "min_cost:", min_cost
return moved_centers
def k_mediods(X,initial_center_idxs,max_iters,distance_matrix):
m = np.size(X,0)
K = np.size(initial_center_idxs,0)
center_idxs = initial_center_idxs
previous_center_idxs = center_idxs
idx = np.zeros(m,dtype=int)
for i in xrange(max_iters):
idx = find_closest_centers(X,center_idxs,distance_matrix)
previous_center_idxs = center_idxs
center_idxs = compute_centers(X,idx,center_idxs,distance_matrix)
if (previous_center_idxs == center_idxs).all() == True:
break;
if (previous_center_idxs == center_idxs).all() == False:
idx = find_closest_centers(X,center_idxs,distance_matrix)
return (center_idxs,idx)
def model_cost(X, idx, center_idxs,distance_matrix):
K = np.size(center_idxs,0)
total_cost = 0.0;
for k in center_idxs:
(k_cluster_x_indxs,) = np.where(idx[:]==k)
#print k_cluster_x_indxs
cost = 0.0
for x_indx in k_cluster_x_indxs:
cost += distance_matrix[k,x_indx]
total_cost += cost
return total_cost
def initial_random_centers_cost_minimization(X,K,distance_matrix,random_shuffel_max_iters,kmediods_max_iters):
min_cost = np.inf
for i in xrange(random_shuffel_max_iters):
(initial_centers,initial_center_idxs) = initial_random_centers(X,K)
(center_idxs,idx) = k_mediods(X,initial_center_idxs,kmediods_max_iters,distance_matrix)
total_cost = model_cost(X,idx,center_idxs,distance_matrix)
if min_cost > total_cost:
min_cost = total_cost
min_center_idxs = center_idxs
return (min_center_idxs,min_cost)
def elbow_method_choose_k_with_random_init_cost_minimization(X,max_K,distance_matrix,random_shuffel_max_iters,kmediods_max_iters):
cost_array = np.zeros(max_K,dtype=float)
for K in xrange(1,max_K+1):
(init_center_idxs,cost) = initial_random_centers_cost_minimization(X,K,distance_matrix,random_shuffel_max_iters,kmediods_max_iters)
(center_idxs,idx) = k_mediods(X,init_center_idxs,kmediods_max_iters,distance_matrix)
total_cost = model_cost(X,idx,center_idxs,distance_matrix)
cost_array[K-1] = total_cost
#print_clusters(X,idx,center_idxs)
print "cost:", total_cost, "K:",K
K_vals = np.linspace(1,max_K,max_K)
pl.plot(K_vals,cost_array)
pl.plot(K_vals,cost_array,'rx',label='distortion')
pl.show()
def compute_symmetric_distance(X,distance_function):
m = np.size(X,0)
dist = np.zeros((m,m),dtype=float)
for i in xrange(m):
for j in xrange(i+1,m):
dist[i,j] = distance_function(X[i], X[j])
dist[j,i] = dist[i,j]
#print "distance between", X[i], "and ", X[j], ":",dist[i,j]
return dist
def print_clusters(X,idx,center_idxs):
for k in center_idxs:
print "Cluster:", X[k]
print X[idx[:] == k]
K = 20
random_shuffel_max_iters = 100
kmediods_max_iters = 100
#X = np.array(['ape', 'appel', 'apple', 'peach', 'puppy'])
X = np.array(
['the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have',
'I', 'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you',
'do', 'at', 'this', 'but', 'his', 'by', 'from', 'they', 'we',
'say', 'her', 'she', 'or', 'an', 'will', 'my', 'one', 'all',
'would', 'there', 'their', 'what', 'so', 'up', 'out', 'if',
'about', 'who', 'get', 'which', 'go', 'me', 'when', 'make',
'can', 'like', 'time', 'no', 'just', 'him', 'know', 'take',
'people', 'into', 'year', 'your', 'good', 'some', 'could',
'them', 'see', 'other', 'than', 'then', 'now', 'look',
'only', 'come', 'its', 'over', 'think', 'also', 'back',
'after', 'use', 'two', 'how', 'our', 'work', 'first', 'well',
'way', 'even', 'new', 'want', 'because', 'any', 'these',
'give', 'day', 'most', 'us'])
distance_matrix = compute_symmetric_distance(X,distance_function=jellyfish.levenshtein_distance)
(initial_centers,initial_center_idxs) = initial_random_centers(X,K)
(center_idxs,idx) = k_mediods(X,initial_center_idxs,kmediods_max_iters,distance_matrix)
print_clusters(X,idx,center_idxs)
total_cost = model_cost(X,idx,center_idxs,distance_matrix)
print "model cost: ", total_cost
#elbow_method_choose_k_with_random_init_cost_minimization(X,K,distance_matrix,random_shuffel_max_iters,kmediods_max_iters)
new_K = 10
(min_center_idxs,min_cost) = initial_random_centers_cost_minimization(X,new_K,distance_matrix,random_shuffel_max_iters,kmediods_max_iters)
print "min model cost: ", min_cost
(center_idxs,idx) = k_mediods(X,min_center_idxs,kmediods_max_iters,distance_matrix)
print_clusters(X,idx,center_idxs)
total_cost = model_cost(X,idx,center_idxs,distance_matrix)
print "model cost: ", total_cost