Answer key · 12 questions

Python k-means clustering from scratch Exercises With Answers

All 12 questions for Python k-means clustering from scratch in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

In k-means, what does k represent?

  1. The number of features
  2. The number of clusterscorrect
  3. The learning rate
  4. The number of training labels

Answer: The number of clusters

Why: k is chosen before the loop and determines how many centroids are maintained.

02 · Multiple choice

What happens after every point is assigned to a centroid?

  1. Each centroid moves to the mean of its assigned pointscorrect
  2. The distance function is removed
  3. The points are shuffled
  4. The number of clusters doubles

Answer: Each centroid moves to the mean of its assigned points

Why: The update step computes the coordinate-wise mean of each cluster.

03 · Multiple choice

Why scale features before k-means?

  1. It removes the need for centroids
  2. It guarantees the global optimum
  3. Large-scale features can dominate the distancecorrect
  4. Scaling adds more clusters

Answer: Large-scale features can dominate the distance

Why: A feature measured in thousands can overwhelm one measured between zero and one.

04 · Multiple choice

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])))
  1. 2
  2. 0.5
  3. 1correct
  4. 0

Answer: 1

Why: The point is closer to the second centroid at (5, 5).

05 · Fill in the blank

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.

06 · Fill in the blank

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.

07 · Fill in the blank

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.

08 · Fill in the blank

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.

09 · Predict output

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.

10 · Predict output

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.

11 · Predict output

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).

12 · Predict output

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.

Practice this lesson