February 2018
Intermediate to advanced
378 pages
10h 14m
English
Calculate new centroids of clusters:
var centroidsCount = [Double](repeating: 0.0, count: k)
let rowStub = [Double](repeating: 0.0, count: d)
var centroidsCumulative = [[Double]](repeating: rowStub, count: k)
for (point, clusterID) in zip(data, clusters) {
centroidsCount[clusterID] += 1
centroidsCumulative[clusterID] = vecAdd(centroidsCumulative[clusterID], point)
}
var newCentroids = centroidsCumulative
for (j, row) in centroidsCumulative.enumerated() {
for (i, element) in row.enumerated() {
let new = element/centroidsCount[j]
assert(!new.isNaN)
newCentroids[j][i] = new
}
}
After this, we have to check whether the new centroids are different from those previously calculated. If they are different, we perform another iteration ...
Read now
Unlock full access