TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

K-Means Assignment Step

Classic ML
Easy

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.

Formula

For each point p, find the centroid c that minimizes the squared Euclidean distance:

assignment(p)=arg⁡min⁡j∑d=1D(pd−cj,d)2\text{assignment}(p) = \arg\min_j \sum_{d=1}^{D} (p_d - c_{j,d})^2assignment(p)=argjmin​d=1∑D​(pd​−cj,d​)2

Return one integer centroid index for each input point.

Loading visualization...

Examples

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]

Hint 1

Compute squared distance with zip for each point-centroid pair.

Hint 2

Keep the first centroid when distances tie by updating only for a strictly smaller distance.

Requirements

  • For each point, compute the squared Euclidean distance to every centroid
  • Assign the point to the centroid with the smallest distance
  • If distances are tied, assign to the centroid with the smallest index
  • Return a list of integer cluster indices

Constraints

  • All points and centroids have the same dimensionality
  • At least one point and one centroid
  • Return a list of integers with the same length as points
  • Time limit: 300 ms
Try Similar Problems
K Means Centroid UpdateKnn DistanceEuclidean DistanceSilhouette ScoreManhattan Distance

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.
PrevNext

K-Means Assignment Step

Classic ML
Easy

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.

Formula

For each point p, find the centroid c that minimizes the squared Euclidean distance:

assignment(p)=arg⁡min⁡j∑d=1D(pd−cj,d)2\text{assignment}(p) = \arg\min_j \sum_{d=1}^{D} (p_d - c_{j,d})^2assignment(p)=argjmin​d=1∑D​(pd​−cj,d​)2

Return one integer centroid index for each input point.

Loading visualization...

Examples

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]

Hint 1

Compute squared distance with zip for each point-centroid pair.

Hint 2

Keep the first centroid when distances tie by updating only for a strictly smaller distance.

Requirements

  • For each point, compute the squared Euclidean distance to every centroid
  • Assign the point to the centroid with the smallest distance
  • If distances are tied, assign to the centroid with the smallest index
  • Return a list of integer cluster indices

Constraints

  • All points and centroids have the same dimensionality
  • At least one point and one centroid
  • Return a list of integers with the same length as points
  • Time limit: 300 ms
Try Similar Problems
K Means Centroid UpdateKnn DistanceEuclidean DistanceSilhouette ScoreManhattan Distance

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.