Choosing K Practice Problem
This data science coding problem helps you practice K-Nearest Neighbors, choosing k, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of K-Nearest Neighbors.
- Problem ID: 184
- Problem key: 184-choosing-k
- URL: https://datacrack.app/solve/184-choosing-k
- Difficulty: medium
- Topic: K-Nearest Neighbors
- Module: Supervised Learning
Problem Statement
# 🧩 Choosing K
---
### 🎯 Goal
Compare KNN predictions made with different values of `k`.
---
### 📖 Introduction
`k` controls how many nearby training samples KNN listens to.
- Small `k`: uses only a few nearest neighbors, so the prediction is strongly influenced by the closest samples.
- Large `k`: uses more neighbors, so the prediction considers a wider group of samples and becomes less sensitive to individual points.
There is no universally best value. This problem predicts the same query with several `k` values so you can see the tradeoff.
---
### 💻 Task
Implement `compare_k_predictions(X, y, query, k_values)`.
Your function should:
- Calculate and sort all query-to-training distances once.
- For each value in `k_values`, select that many nearest labels.
- Use majority voting for each selection.
- Return a dictionary whose keys are the requested `k` values converted to strings.
- Break label ties alphabetically.
---
### 📥 Input / 📤 Output
**Input:** `X`, `y`, one `query`, and a list `k_values`.
**Output:** one prediction for every requested `k`.
---
### 🧩 Starter Code
```python
def compare_k_predictions(X, y, query, k_values):
def distance(row):
# Calculate distance from row to query
pass
def vote(labels):
# Return the label with the most votes
pass
# Your code here
pass
```
---
### 💡 Example
**Input**
```python
compare_k_predictions(
X=[
[0],
[1],
[2],
[8],
[9]
],
y=[
"A",
"A",
"B",
"B",
"B"
],
query=[2.1],
k_values=[1, 3, 5]
)
```
**Expected output**
```python
{
"1": "B",
"3": "A",
"5": "B"
}
```
---
### ⚠️ Common Mistakes
- Using the same neighbors for every value of `k`.
- Sorting labels instead of sorting samples by distance.
- Assuming a larger `k` is always better.
Starter Code
def compare_k_predictions(X, y, query, k_values):
def distance(row):
# Calculate distance from row to query
pass
def vote(labels):
# Return the label with the most votes
pass
# Your code here
pass