k-Means Inertia

~8 mincode completion

Implement kmeans_inertia(X, centroids, labels) returning as a float.

Where this shows up

~8 min

1 employer weight this skill

1 health and bio company. Top match scores 87.

Python
import numpy as np


def kmeans_inertia(X, centroids, labels):
    """
    The k-means objective for a given assignment.

    Args:
        X:         array of shape (n, d)
        centroids: array of shape (k, d)
        labels:    integer array of shape (n,), the cluster of each point

    Returns:
        float: sum of squared distances from each point to its assigned centroid
    """
    # YOUR CODE HERE
    pass

Run your code to see results

⌘↵ runs against the visible tests

Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.