Challenge: Mahalanobis Distance in Practice
Swipe to start coding
You are given a small 2D dataset. Your goal is to compute the Mahalanobis distance of each observation from the data center and use it to detect outliers.
Steps:
- Compute the mean vector of the dataset.
- Compute the covariance matrix and its inverse.
- For each observation, compute Mahalanobis distance using the formula:
[
D(x) = \sqrt{(x - \mu)^T \Sigma^{-1} (x - \mu)}
]
4. Store all distances in an array distances.
5. Classify points as outliers if distance > threshold (use threshold = 2.5).
6. Print both arrays (distances and outliers) for verification.
Use NumPy only.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain this in simpler terms?
What are the main takeaways from this?
Can you give me a real-world example?
Awesome!
Completion rate improved to 4.55
Challenge: Mahalanobis Distance in Practice
Swipe to show menu
Swipe to start coding
You are given a small 2D dataset. Your goal is to compute the Mahalanobis distance of each observation from the data center and use it to detect outliers.
Steps:
- Compute the mean vector of the dataset.
- Compute the covariance matrix and its inverse.
- For each observation, compute Mahalanobis distance using the formula:
[
D(x) = \sqrt{(x - \mu)^T \Sigma^{-1} (x - \mu)}
]
4. Store all distances in an array distances.
5. Classify points as outliers if distance > threshold (use threshold = 2.5).
6. Print both arrays (distances and outliers) for verification.
Use NumPy only.
Solution
Thanks for your feedback!
single