In K-Nearest Neighbors, what happens if you increase the value of K too much?
Think about how using many neighbors affects the decision boundary.
Increasing K means the model looks at many neighbors, smoothing predictions and possibly missing small but important details, leading to underfitting.
Given the following distances and labels of neighbors, what will the KNN predict for K=3?
neighbors = [(0.5, 'A'), (0.7, 'B'), (0.9, 'A'), (1.2, 'B')]
Count the labels of the 3 closest neighbors.
The 3 closest neighbors have labels 'A', 'B', and 'A'. 'A' appears twice, so the prediction is 'A'.
You have a dataset and want to choose the best K for KNN. Which method is the most reliable to find the best K?
Think about how to test model performance fairly on unseen data.
Cross-validation tests the model on different splits of data, giving a better estimate of performance and helping choose the best K.
How does increasing K in KNN affect bias and variance?
Think about how smoothing predictions affects errors.
Higher K smooths predictions, making the model simpler (higher bias) but more stable (lower variance).
What error does the following code produce when calculating Euclidean distance between two points?
def euclidean_distance(p1, p2):
return sum((x - y) ** 2 for x, y in zip(p1)) ** 0.5
print(euclidean_distance([1, 2], [4, 6]))Check how zip is used in the code.
zip(p1) produces single-element tuples, but the code tries to unpack into two variables x and y, causing ValueError: not enough values to unpack (expected 2, got 1).
