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

K-Means Centroid Update

Classic ML
Medium

After assigning each point to a cluster, the centroid update step recomputes each centroid as the mean of all points assigned to it. This is the second half of one K-Means iteration.

Given a list of data points, their cluster assignments, and the number of clusters k, compute the new centroid positions.

Formula

For each cluster j, the new centroid is the mean of all assigned points:

cj=1∣Sj∣∑p∈Sjpc_j = \frac{1}{|S_j|} \sum_{p \in S_j} pcj​=∣Sj​∣1​p∈Sj​∑​p

Where S_j is the set of points assigned to cluster j.

Return k centroids as a list of lists of floats.

Loading visualization...

Examples

Input: points = [[0, 0], [2, 2], [10, 10], [12, 12]], assignments = [0, 0, 1, 1], k = 2

Output: [[1.0, 1.0], [11.0, 11.0]]

Explanation: Each centroid is the coordinate-wise mean of the points assigned to its cluster.

Input: points = [[0, 0], [1, 0], [5, 5], [6, 5], [10, 0]], assignments = [0, 0, 1, 1, 2], k = 3

Output: [[0.5, 0.0], [5.5, 5.0], [10.0, 0.0]]

Hint 1

Accumulate a coordinate sum and point count for each cluster.

Hint 2

Divide each coordinate sum by its cluster count, using a zero vector for an empty cluster.

Requirements

  • For each cluster, compute the mean of all assigned points along each dimension
  • If a cluster has no assigned points, return a zero vector for that centroid
  • Return a list of k centroids, each a list of floats

Constraints

  • assignments[i] is an integer in [0, k-1]
  • All points have the same dimensionality
  • Return a list of k centroids, each a list of floats
  • Time limit: 300 ms
Try Similar Problems
K Means AssignmentSilhouette ScoreKnn DistanceEuclidean DistanceGaussian Naive Bayes

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
PrevNext

K-Means Centroid Update

Classic ML
Medium

After assigning each point to a cluster, the centroid update step recomputes each centroid as the mean of all points assigned to it. This is the second half of one K-Means iteration.

Given a list of data points, their cluster assignments, and the number of clusters k, compute the new centroid positions.

Formula

For each cluster j, the new centroid is the mean of all assigned points:

cj=1∣Sj∣∑p∈Sjpc_j = \frac{1}{|S_j|} \sum_{p \in S_j} pcj​=∣Sj​∣1​p∈Sj​∑​p

Where S_j is the set of points assigned to cluster j.

Return k centroids as a list of lists of floats.

Loading visualization...

Examples

Input: points = [[0, 0], [2, 2], [10, 10], [12, 12]], assignments = [0, 0, 1, 1], k = 2

Output: [[1.0, 1.0], [11.0, 11.0]]

Explanation: Each centroid is the coordinate-wise mean of the points assigned to its cluster.

Input: points = [[0, 0], [1, 0], [5, 5], [6, 5], [10, 0]], assignments = [0, 0, 1, 1, 2], k = 3

Output: [[0.5, 0.0], [5.5, 5.0], [10.0, 0.0]]

Hint 1

Accumulate a coordinate sum and point count for each cluster.

Hint 2

Divide each coordinate sum by its cluster count, using a zero vector for an empty cluster.

Requirements

  • For each cluster, compute the mean of all assigned points along each dimension
  • If a cluster has no assigned points, return a zero vector for that centroid
  • Return a list of k centroids, each a list of floats

Constraints

  • assignments[i] is an integer in [0, k-1]
  • All points have the same dimensionality
  • Return a list of k centroids, each a list of floats
  • Time limit: 300 ms
Try Similar Problems
K Means AssignmentSilhouette ScoreKnn DistanceEuclidean DistanceGaussian Naive Bayes

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

You must run your code first.