Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-034

K-Means Inertia

Problem

Compute k-means inertia: the sum over all points of the squared Euclidean distance to the nearest centroid.

Empty points return 0.0.

Hint

For each point add the smallest squared distance over all centroids.

Test cases

case 1
in: [[[0,0],[0,1],[10,10]],[[0,0.5],[10,10]]]
expected: 0.5
case 2
in: [[[1,1],[2,2],[10,10]],[[1.5,1.5],[10,10]]]
expected: 1
case 3
in: [[[5,5]],[[0,0],[5,5]]]
expected: 0
case 4
in: [[],[[0,0]]]
expected: 0
Starter codePython
def kmeans_inertia(points, centroids):
    # Your code here
    pass

4 test cases · run your code against them in the editor