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.
How does a nearest-neighbor classifier predict?
Answer: By voting among the closest training examples
Why: k-NN looks at the nearest labeled points and combines their labels.
Why scale features before computing distance?
Answer: Large units can dominate the distance
Why: A feature measured in thousands can overwhelm one measured in single digits.
What should happen when neighbor labels tie?
Answer: Use a documented deterministic rule
Why: A tie rule such as nearest tied neighbor keeps predictions reproducible.
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])Answer: ['a', 'a', 'b']
Why: The two points nearest the target are labeled a; the far point is b.
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.
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.
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.
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.
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.
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.
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.
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.