You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# reset cluster groups (note: assignments are not touched yet)
196
+
initial ||foreach(empty!, groups)
200
197
201
198
tcost =0.0
199
+
ch =0
202
200
for j =1:n
203
-
p =1
201
+
p =1# initialize the closest medoid for j
204
202
mv = dist[medoids[1], j]
205
203
206
-
for i =2:k
207
-
v = dist[medoids[i], j]
208
-
if v < mv
204
+
# find the closest medoid for j
205
+
@inboundsfor i =2:k
206
+
m = medoids[i]
207
+
v = dist[m, j]
208
+
# assign if current medoid is closer or if it is j itself
209
+
if (v < mv) || (m == j)
210
+
(v <= mv) ||throw(ArgumentError("sample #$j reassigned from medoid[$p]=#$(medoids[p]) (distance=$mv) to medoid[$i]=#$m (distance=$v); check the distance matrix correctness"))
209
211
p = i
210
212
mv = v
211
213
end
212
214
end
213
215
214
-
if isinit
215
-
assignments[j] = p
216
-
else
217
-
a = assignments[j]
218
-
if p != a
219
-
ch +=1
220
-
end
221
-
assignments[j] = p
222
-
end
223
-
216
+
ch +=!initial && (p != assignments[j])
217
+
assignments[j] = p
224
218
costs[j] = mv
225
219
tcost += mv
226
220
push!(groups[p], j)
227
221
end
228
222
229
-
return (tcost, ch)::Tuple{Float64, Int}
223
+
return (tcost, ch)
230
224
end
231
225
232
226
233
227
# find medoid for a given group
234
-
#
235
-
#TODO: faster way without creating temporary arrays
0 commit comments