This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python k-means clustering from scratch, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
In k-means, what does k represent?
Answer: The number of clusters
Why: k is chosen before the loop and determines how many centroids are maintained.
What happens after every point is assigned to a centroid?
Answer: Each centroid moves to the mean of its assigned points
Why: The update step computes the coordinate-wise mean of each cluster.
Why scale features before k-means?
Answer: Large-scale features can dominate the distance
Why: A feature measured in thousands can overwhelm one measured between zero and one.
What is the output?
def distance(a, b):
return sum((x - y) ** 2 for x, y in zip(a, b)) ** 0.5
centroids = [(0, 0), (5, 5)]
point = (4, 4)
print(min(range(len(centroids)), key=lambda i: distance(point, centroids[i])))Answer: 1
Why: The point is closer to the second centroid at (5, 5).
Complete the Euclidean distance exponent.
def distance(a, b):
return sum((x - y) ** 2 for x, y in zip(a, b)) ** ____
print(round(distance((0, 0), (3, 4)), 1))Answer: 0.5
Why: A square root is the same as raising the sum to the power 0.5.
Complete the nearest-centroid key.
def distance(a, b):
return sum((x - y) ** 2 for x, y in zip(a, b)) ** 0.5
centroids = [(0, 0), (5, 5)]
point = (1, 1)
print(min(range(len(centroids)), key=lambda i: ____(point, centroids[i])))Answer: distance
Why: The key function compares the point with each centroid using the distance function.
Complete the centroid mean denominator.
points = [(0, 0), (0, 1)]
members = points
x_mean = sum(x for x, _ in members) / ____
print(x_mean)Answer: len(members)
Why: The mean divides the coordinate sum by the number of members in the cluster.
Complete the convergence check.
points = [(0, 0), (5, 5)]
labels = [0, 0]
new_labels = [0, 0]
if new_labels == ____:
print("stable")Answer: labels
Why: The loop stops when the newly assigned labels match the previous labels.
What does this code print?
import math
print(math.dist((0, 0), (3, 4)))Answer: 5.0
Why: The Euclidean distance between the points is 5.0.
What does the program output?
def distance(a, b):
return sum((x - y) ** 2 for x, y in zip(a, b)) ** 0.5
centroids = [(0, 0), (5, 5)]
point = (1, 1)
print(min(range(len(centroids)), key=lambda i: distance(point, centroids[i])))Answer: 0
Why: The point is closest to the first centroid, whose index is 0.
What appears on the screen?
points = [(0, 0), (0, 1)]
members = points
centroid = (
sum(x for x, _ in members) / len(members),
sum(y for _, y in members) / len(members),
)
print(tuple(round(value, 1) for value in centroid))Answer: (0.0, 0.5)
Why: The mean of (0, 0) and (0, 1) is (0.0, 0.5).
What is printed?
def distance(a, b):
return sum((x - y) ** 2 for x, y in zip(a, b)) ** 0.5
points = [(0, 0), (0, 1), (5, 5), (5, 6)]
centroids = [(0, 0), (5, 5)]
labels = None
for _ in range(10):
new_labels = [min(range(len(centroids)), key=lambda i: (distance(point, centroids[i]), i)) for point in points]
new_centroids = []
for index in range(len(centroids)):
members = [point for point, label in zip(points, new_labels) if label == index]
new_centroids.append((sum(x for x, _ in members) / len(members), sum(y for _, y in members) / len(members)))
if new_labels == labels:
break
labels = new_labels
centroids = new_centroids
print(labels)Answer: [0, 0, 1, 1]
Why: The first two points stay with centroid 0 and the last two with centroid 1.
Try the questions first
Open the practice page for Python k-means clustering from scratch and answer the same 12 questions with instant feedback.