Effect of Feature Scaling Practice Problem
This data science coding problem helps you practice K-Nearest Neighbors, effect of feature scaling, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of K-Nearest Neighbors.
- Problem ID: 187
- Problem key: 187-effect-of-feature-scaling
- URL: https://datacrack.app/solve/187-effect-of-feature-scaling
- Difficulty: medium
- Topic: K-Nearest Neighbors
- Module: Supervised Learning
Problem Statement
# 🧩 Effect of Feature Scaling
---
### 🎯 Goal
Show how feature scaling can change KNN distances and predictions.
---
### 📖 Introduction
KNN treats every numeric feature as part of a distance calculation. A feature with much larger numbers can dominate that distance, even when it is not more important.
Min-max scaling changes each feature to the range from `0` to `1`. This gives features comparable numeric ranges before KNN measures distance.
---
### 💻 Task
Implement `compare_scaled_predictions(X, y, query, k)`.
Your function should:
- Predict with KNN using the original feature values.
- Min-max scale every feature using the minimum and maximum from `X`.
- Scale `query` using those same training-set values.
- Predict again with scaled values.
- Return `{"without_scaling": ..., "with_scaling": ...}`.
- Break vote ties alphabetically.
---
### 📥 Input / 📤 Output
**Input:** training `X`, labels `y`, one `query`, and `k`.
**Output:** the prediction before and after scaling.
---
### 🧩 Starter Code
```python
def compare_scaled_predictions(X, y, query, k):
def distance(row, target):
pass
def predict(features, target):
pass
def scale(row):
pass
# Your code here
pass
```
---
### 💡 Example
**Input**
```python
compare_scaled_predictions(
X=[
[0, 0],
[10, 1000]
],
y=[
"A",
"B"
],
query=[1, 800],
k=1
)
```
**Expected output**
```python id="1z7m8p"
{
"without_scaling": "B",
"with_scaling": "A"
}
```
---
### ⚠️ Common Mistakes
- Scaling the query with different minimum and maximum values.
- Including the query when calculating training feature ranges.
- Forgetting to handle a feature whose minimum equals its maximum.
Starter Code
def compare_scaled_predictions(X, y, query, k):
def distance(row, target):
pass
def predict(features, target):
pass
def scale(row):
pass
# Your code here
pass