Distance Calculation Practice Problem
This data science coding problem helps you practice K-Nearest Neighbors, distance calculation, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of K-Nearest Neighbors.
- Problem ID: 186
- Problem key: 186-distance-calculation
- URL: https://datacrack.app/solve/186-distance-calculation
- Difficulty: easy
- Topic: K-Nearest Neighbors
- Module: Supervised Learning
Problem Statement
# 🧩 Distance Calculation
---
### 🎯 Goal
Calculate the Euclidean distance between two samples.
---
### 📖 Introduction
**K-Nearest Neighbors (KNN)** is a supervised machine-learning algorithm that predicts by comparing a new sample with examples it has already seen.
Before KNN can decide which examples are similar, it needs a way to measure how far apart two samples are. The usual choice is **Euclidean distance**: the straight-line distance between two points.
A smaller distance means KNN considers the samples more similar. A distance of `0` means the samples have exactly the same feature values.
---
### 💻 Task
Implement `euclidean_distance(sample_a, sample_b)`.
Your function should:
- Calculate Euclidean distance feature by feature.
- Work with any number of numeric features.
- Return the result rounded to 6 decimals.
---
### 📥 Input / 📤 Output
**Input**
- `sample_a`: a list of numeric feature values.
- `sample_b`: a list of numeric feature values with the same length.
**Output**
- The Euclidean distance as a float.
---
### 🧩 Starter Code
```python
def euclidean_distance(sample_a, sample_b):
# Your code here
pass
```
---
### 💡 Example
```python
euclidean_distance([1, 2], [4, 6])
```
Expected output:
```python
5.0
```
---
### ⚠️ Common Mistakes
- Adding raw differences without squaring them.
- Forgetting the square root at the end.
- Comparing values from different feature positions.
Starter Code
def euclidean_distance(sample_a, sample_b):
# Your code here
pass