Neural Mastery
← Practice
K-Means++ Min Squared Centroid Distanceseasy

K-Means++ Min Squared Centroid Distances

easy Classical ML from Scratch 10–15 min

NumPy allowed · plain Python earns 10 bonus points

Compute nearest squared centroid distances for K-Means++ probabilistic initialization.

Related Skills

Task

Implement kmeans_plus_plus_distances(points, centroids): for each point x, find min(sum((x[j] - c[j])**2 for c in centroids)). Return list of minimum squared distances rounded to 4 decimal places.

Function Signature

kmeans_plus_plus_distances(points: list[list[float]], centroids: list[list[float]]) -> list[float]

Examples

Example 1: Single centroid origin
Input: points = [[1, 0], [0, 2]] centroids = [[0, 0]]
Output: [1, 4]
Example 2: Two centroids pick closest
Input: points = [[1, 1], [9, 9]] centroids = [[0, 0], [10, 10]]
Output: [2, 2]

Constraints

  • points and centroids are non-empty lists of float vectors of equal length
Python3Saved ✓
def kmeans_plus_plus_distances(points: list[list[float]], centroids: list[list[float]]) -> list[float]:
"""Compute min squared Euclidean distance from each point to nearest centroid.
Round floats to 4 decimal places.
"""
pass

Case 1: Single centroid origin
Input: points = [[1, 0], [0, 2]] centroids = [[0, 0]]
Expected: [1, 4]
Case 2: Two centroids pick closest
Input: points = [[1, 1], [9, 9]] centroids = [[0, 0], [10, 10]]
Expected: [2, 2]