0
0
ML Pythonprogramming~20 mins

K-Nearest Neighbors (KNN) in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
KNN Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How K affects KNN predictions

In K-Nearest Neighbors, what happens if you increase the value of K too much?

AThe model becomes too complex and fits noise, causing overfitting.
BThe model ignores all neighbors and predicts randomly.
CThe model always predicts the class of the closest neighbor only.
DThe model becomes too simple and may miss important patterns, causing underfitting.
Attempts:
2 left
Predict Output
intermediate
2:00remaining
Output of KNN prediction with given neighbors

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')]
A'C'
B'B'
C'A'
DError due to invalid neighbors
Attempts:
2 left
Hyperparameter
advanced
2:00remaining
Choosing the best K value

You have a dataset and want to choose the best K for KNN. Which method is the most reliable to find the best K?

AUse the training data accuracy for each K and pick the highest.
BUse cross-validation to test different K values and pick the one with best average validation accuracy.
CPick the smallest K to avoid overfitting.
DPick the largest K to avoid underfitting.
Attempts:
2 left
Metrics
advanced
2:00remaining
Effect of K on bias and variance

How does increasing K in KNN affect bias and variance?

AIncreasing K increases bias and decreases variance.
BIncreasing K decreases bias and increases variance.
CIncreasing K increases both bias and variance.
DIncreasing K decreases both bias and variance.
Attempts:
2 left
🔧 Debug
expert
2:00remaining
Identifying error in KNN distance calculation

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]))
AValueError: not enough values to unpack (expected 2, got 1)
BTypeError: unsupported operand type(s) for -: 'int' and 'list'
CNameError: name 'y' is not defined
D0.0 (incorrect distance)
Attempts:
2 left