Build a KNN Classifier Practice Problem
This data science coding problem helps you practice K-Nearest Neighbors, build a knn classifier, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of K-Nearest Neighbors.
- Problem ID: 183
- Problem key: 183-build-a-knn-classifier
- URL: https://datacrack.app/solve/183-build-a-knn-classifier
- Difficulty: medium
- Topic: K-Nearest Neighbors
- Module: Supervised Learning
Problem Statement
# 🧩 Build a KNN Classifier
---
### 🎯 Goal
Combine distance calculation, neighbor selection, and majority voting into a complete KNN classifier.
---
### 📖 Introduction
You have now built each part of KNN separately. A full classifier repeats the same pipeline for every new query:
1. Calculate its distance to every training row.
2. Keep the `k` smallest distances.
3. Vote using their labels.
4. Return the winning label.
Unlike many machine-learning algorithms, KNN does not fit a formula during training. It stores the training examples and does this work when asked to predict.
---
### 💻 Task
Implement `knn_predict(X, y, queries, k)`.
Your function should:
- Predict a label for every query in `queries`.
- Use Euclidean distance.
- Select the `k` nearest training samples.
- Use majority voting.
- Return predictions in the same order as `queries`.
- Break vote ties alphabetically.
---
### 📥 Input / 📤 Output
**Input:** training features `X`, labels `y`, new samples `queries`, and `k`.
**Output:** a list of predicted labels.
---
### 🧩 Starter Code
```python
def knn_predict(X, y, queries, k):
# X: training feature matrix
# y: training labels
# queries: new samples to predict
# k: number of nearest neighbors
def distance(row, target):
# Calculate Euclidean distance
pass
def vote(labels):
# Return the label with the most votes
pass
def predict_one(query):
# Predict one query point using KNN
pass
# Predict all queries
pass
```
---
### 💡 Example
**Input**
```python
knn_predict(
X=[
[1, 1],
[2, 2],
[5, 5],
[6, 6]
],
y=[
"A",
"A",
"B",
"B"
],
queries=[
[1.5, 1.5],
[5.5, 5.5]
],
k=3
)
```
**Expected output**
```python
[
"A",
"B"
]
```
---
### ⚠️ Common Mistakes
- Reusing the neighbors from the first query for later queries.
- Forgetting to return one result for every query.
- Voting before sorting by distance.
Starter Code
def knn_predict(X, y, queries, k):
# X: training feature matrix
# y: training labels
# queries: new samples to predict
# k: number of nearest neighbors
def distance(row, target):
# Calculate Euclidean distance
pass
def vote(labels):
# Return the label with the most votes
pass
def predict_one(query):
# Predict one query point using KNN
pass
# Predict all queries
pass