K-Means clustering alternates between two steps: assigning points to clusters and updating centroids. The assignment step assigns each data point to the nearest centroid based on squared Euclidean distance.
Given a list of data points and a list of current centroid positions, assign each point to the nearest centroid.
For each point p, find the centroid c that minimizes the squared Euclidean distance:
assignment(p)=argjmind=1∑D(pd−cj,d)2Return one integer centroid index for each input point.
Input: points = [[1, 1], [1, 2], [10, 10], [10, 11]], centroids = [[0, 0], [11, 11]]
Output: [0, 0, 1, 1]
Explanation: The first two points are closest to centroid 0 and the final two are closest to centroid 1.
Input: points = [[0, 0], [5, 5], [10, 0]], centroids = [[0, 0], [5, 5], [10, 0]]
Output: [0, 1, 2]
Compute squared distance with zip for each point-centroid pair.
Keep the first centroid when distances tie by updating only for a strictly smaller distance.
Sign in to take notes on this problem
Accepts: array
Accepts: array
K-Means clustering alternates between two steps: assigning points to clusters and updating centroids. The assignment step assigns each data point to the nearest centroid based on squared Euclidean distance.
Given a list of data points and a list of current centroid positions, assign each point to the nearest centroid.
For each point p, find the centroid c that minimizes the squared Euclidean distance:
assignment(p)=argjmind=1∑D(pd−cj,d)2Return one integer centroid index for each input point.
Input: points = [[1, 1], [1, 2], [10, 10], [10, 11]], centroids = [[0, 0], [11, 11]]
Output: [0, 0, 1, 1]
Explanation: The first two points are closest to centroid 0 and the final two are closest to centroid 1.
Input: points = [[0, 0], [5, 5], [10, 0]], centroids = [[0, 0], [5, 5], [10, 0]]
Output: [0, 1, 2]
Compute squared distance with zip for each point-centroid pair.
Keep the first centroid when distances tie by updating only for a strictly smaller distance.
Sign in to take notes on this problem
Accepts: array
Accepts: array