Decision Boundaries Practice Problem
This data science coding problem helps you practice K-Nearest Neighbors, decision boundaries, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of K-Nearest Neighbors.
- Problem ID: 185
- Problem key: 185-decision-boundaries
- URL: https://datacrack.app/solve/185-decision-boundaries
- Difficulty: medium
- Topic: K-Nearest Neighbors
- Module: Supervised Learning
Problem Statement
# 🧩 Decision Boundaries
---
### 🎯 Goal
Generate KNN predictions over 2D query points to reveal its decision regions.
---
### 📖 Introduction
A **decision boundary** is where a model changes from predicting one class to predicting another. KNN has no single line or equation for that boundary. Instead, every location is classified from its nearest labeled samples.
If we predict a dense grid of points and color each point by its prediction, the colored regions show KNN's decision boundary.
---
### 💻 Task
Implement `knn_grid_predictions(X, y, grid, k)`.
Your function should:
- Treat every point in `grid` as a new query.
- Find its `k` nearest rows from `X` using Euclidean distance.
- Majority vote their labels.
- Return predictions in the same order as `grid`.
- Break vote ties alphabetically.
---
### 📥 Input / 📤 Output
**Input:** 2D training features `X`, labels `y`, a list of 2D grid points, and `k`.
**Output:** one predicted label per grid point.
---
### 🧩 Starter Code
```python
def knn_grid_predictions(X, y, grid, k):
def distance(row, target):
# Calculate Euclidean distance
pass
def vote(labels):
# Return the label with the most votes
pass
def predict_one(query):
# Predict the label of one query point
pass
# Predict all points in the grid
pass
```
---
### 💡 Example
**Input**
```python
knn_grid_predictions(
X=[
[0, 0],
[0, 2],
[3, 0],
[3, 2]
],
y=[
"A",
"A",
"B",
"B"
],
grid=[
[0.5, 1],
[2.5, 1]
],
k=1
)
```
**Expected output**
```python
[
"A",
"B"
]
```
---
### ⚠️ Common Mistakes
- Predicting only one grid point.
- Returning labels in a different order from `grid`.
- Using all training labels instead of only the nearest `k`.
Starter Code
def knn_grid_predictions(X, y, grid, k):
def distance(row, target):
# Calculate Euclidean distance
pass
def vote(labels):
# Return the label with the most votes
pass
def predict_one(query):
# Predict the label of one query point
pass
# Predict all points in the grid
pass