Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
X_train = np.array([[0, 0], [1, 3], [3, 0], [3, 3], [5, 5]])
y_train = np.array([0, 0, 0, 1, 1])
x = np.array([3, 2])
dist = np.sqrt(((X_train - x) ** 2).sum(axis=1))
order = np.argsort(dist)
print(y_train[order[0]], np.bincount(y_train[order[:3]]).argmax())1 0
- Expecting
1 1: the nearest point[3, 3]is class1, but the next two,[3, 0]and[1, 3], are class0, so three neighbors vote0. - Expecting
0for one neighbor because class0has more training points: with one neighbor only the closest point counts, and[3, 3]at distance1.0is class1.
- Which label would this code return with all five points as neighbors, and why?
- How would weighting each vote by inverse distance change the answer for three neighbors?