February 2018
Intermediate to advanced
378 pages
10h 14m
English
After the initial choice of centroids, we need to iterate over data points and update information about cluster assignment according to the distance to the closest centroid:
for (pointIndex, point) in data.enumerated() {
var minDistance = Double.infinity
for (clusterID, centroid) in centroids.enumerated() {
let distance = pow(distanceMetric(point, centroid), 2)
Remember the newly calculated distance if it is less than the previously saved minimum distance for the corresponding data point:
if minDistance > distance {
clusters[pointIndex] = clusterID
minDistance = distance
}
}
Save information about WCSS for the future:
WCSS += minDistance }
Read now
Unlock full access