All problems
Lloyd-Max One Update
Problem
Perform one Lloyd-Max iteration. Assign every sample to its nearest centroid, breaking ties toward the lower index, then move each centroid to the mean of the samples assigned to it. A centroid that receives no samples keeps its old value. Return the updated centroid list.
Hint
The assignment step uses the old centroids; the update step uses the new assignments.
Test cases
case 1
in: [[0,1,2,10],[0,5]]
expected: [1,10]
case 2
in: [[1,2,3,10,11,12],[2,11]]
expected: [2,11]
case 3
in: [[0,0.4,0.6,1],[0.25,0.75]]
expected: [0.2,0.8]
case 4
in: [[5],[0,10]]
expected: [5,10]
case 5
in: [[-3,-1,4,6],[0,5]]
expected: [-2,5]
Starter codePython
def lloyd_max_update(samples, centroids):
# Your code here
pass5 test cases · run your code against them in the editor