Answer key · 12 questions

Python nearest-neighbor classifier Exercises With Answers

All 12 questions for Python nearest-neighbor classifier 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 nearest-neighbor classifier, 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

How does a nearest-neighbor classifier predict?

  1. By voting among the closest training examplescorrect
  2. By sorting the alphabet
  3. By hashing the label
  4. By fitting a neural network for many epochs

Answer: By voting among the closest training examples

Why: k-NN looks at the nearest labeled points and combines their labels.

02 · Multiple choice

Why scale features before computing distance?

  1. Scaling makes the labels alphabetical
  2. Large units can dominate the distancecorrect
  3. It guarantees perfect accuracy
  4. It removes the need for neighbors

Answer: Large units can dominate the distance

Why: A feature measured in thousands can overwhelm one measured in single digits.

03 · Multiple choice

What should happen when neighbor labels tie?

  1. Ignore the target point
  2. Return every label as correct
  3. Use a documented deterministic rulecorrect
  4. Pick a random label every time

Answer: Use a documented deterministic rule

Why: A tie rule such as nearest tied neighbor keeps predictions reproducible.

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

points = [((1, 1), "a"), ((4, 4), "b"), ((1, 2), "a")]
target = (1, 1.5)
ranked = sorted(points, key=lambda item: distance(target, item[0]))
print([label for _, label in ranked])
  1. ['a', 'b']
  2. ['b', 'a', 'a']
  3. ['b']
  4. ['a', 'a', 'b']correct

Answer: ['a', 'a', 'b']

Why: The two points nearest the target are labeled a; the far point is b.

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(distance((1, 1), (4, 5)))

Answer: 0.5

Why: Raising the sum of squared differences to 0.5 takes the square root.

06 · Fill in the blank

Complete the distance function used to sort the points.

def distance(a, b):
    return sum((x - y) ** 2 for x, y in zip(a, b)) ** 0.5

points = [((1, 1), "a"), ((4, 4), "b")]
target = (1, 1.5)
ranked = sorted(points, key=lambda item: ____(target, item[0]))
print(ranked[0][1])

Answer: distance

Why: The key function compares the target with each training point.

07 · Fill in the blank

Complete the Counter method that returns the most common label.

from collections import Counter

labels = ["a", "a", "b"]
winner = Counter(labels).____(1)[0][0]
print(winner)

Answer: most_common

Why: most_common(1) returns a list with the top label and its count.

08 · Fill in the blank

Complete the pairing function in the distance loop.

a = (1, 2)
b = (4, 6)
print(sum((x - y) ** 2 for x, y in ____(a, b)))

Answer: zip

Why: zip yields corresponding coordinates from the two points.

09 · Predict output

What does this code print?

def distance(a, b):
    return sum((x - y) ** 2 for x, y in zip(a, b)) ** 0.5

print(distance((1, 1), (4, 5)))

Answer: 5.0

Why: The differences are 3 and 4, so the distance 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

points = [((2, 2), "a"), ((5, 5), "b")]
target = (4.5, 4.5)
ranked = sorted(points, key=lambda item: distance(target, item[0]))
print(ranked[0][1])

Answer: b

Why: The point (5, 5) is closer to the target, so its label b is printed.

11 · Predict output

What appears on the screen?

from collections import Counter

labels = ["a", "a", "b"]
print(Counter(labels).most_common(1)[0][0])

Answer: a

Why: Label a appears twice, so it wins the majority vote.

12 · Predict output

What is printed?

def distance(a, b):
    return sum((x - y) ** 2 for x, y in zip(a, b)) ** 0.5

print(round(distance((30, 30000), (31, 30000)), 1), round(distance((30, 30000), (30, 31000)), 1))

Answer: 1.0 1000.0

Why: The raw-unit distances are 1.0 and 1000.0, showing why scaling matters.

Try the questions first

Open the practice page for Python nearest-neighbor classifier and answer the same 12 questions with instant feedback.

Practice this lesson