IQR Outlier Detection Practice Problem
This data science coding problem helps you practice Outlier Detection & Treatment, iqr outlier detection, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Outlier Detection & Treatment.
- Problem ID: 161
- Problem key: 161-iqr-outlier-detection
- URL: https://datacrack.app/solve/161-iqr-outlier-detection
- Difficulty: easy
- Topic: Outlier Detection & Treatment
- Module: Data Cleaning
Problem Statement
# IQR Outlier Detection
### 🎯 Goal
Detect outliers in a numeric column using the Interquartile Range (IQR) method.
### 💻 Task
Implement `detect_outliers_iqr(data, column)` that:
1. Converts the input dictionary to a DataFrame
2. Computes Q1 (25th percentile), Q3 (75th percentile), and IQR = Q3 − Q1
3. Defines outlier bounds as: lower = Q1 − 1.5 × IQR, upper = Q3 + 1.5 × IQR
4. Identifies values outside these bounds as outliers
5. Returns a dictionary with the bounds and detected outliers
---
### 📥 Input
- `data`: A dictionary where keys are column names and values are lists of numbers
- `column`: The name of the column to check for outliers
### 📤 Output
- A dictionary with:
- `"lower_bound"` (float, rounded to 2 decimals)
- `"upper_bound"` (float, rounded to 2 decimals)
- `"outlier_indices"` (list of integer indices)
- `"outliers"` (list of outlier values)
---
### 🧩 Starter Code
```python
import pandas as pd
import numpy as np
def detect_outliers_iqr(data, column):
"""
Detect outliers using the IQR method.
Args:
data (dict): Input data as dictionary (from JSON)
column (str): Column name to check for outliers
Returns:
dict: Dictionary with lower_bound, upper_bound, outlier_indices, outliers
"""
# TODO: Convert input dictionary to a DataFrame
# TODO: Compute Q1, Q3, and IQR
# TODO: Calculate lower and upper bounds
# TODO: Find outlier indices and values
# TODO: Return result dictionary
pass
```
---
### 💡 Examples
**Example 1:** Single upper outlier
```python
data = {"values": [1, 2, 3, 4, 5, 6, 7, 8, 9, 100]}
detect_outliers_iqr(data, "values")
```
```
{
"lower_bound": -3.5,
"upper_bound": 14.5,
"outlier_indices": [9],
"outliers": [100]
}
```
**Example 2:** Outliers on both ends
```python
data = {"values": [-100, 2, 4, 6, 8, 10, 12, 14, 16, 100]}
detect_outliers_iqr(data, "values")
```
```
{
"lower_bound": -9.0,
"upper_bound": 27.0,
"outlier_indices": [0, 9],
"outliers": [-100, 100]
}
```Starter Code
import pandas as pd
import numpy as np
def detect_outliers_iqr(data, column):
"""
Detect outliers using the IQR method.
Args:
data (dict): Input data as dictionary (from JSON)
column (str): Column name to check for outliers
Returns:
dict: Dictionary with lower_bound, upper_bound, outlier_indices, outliers
"""
# TODO: Convert input dictionary to a DataFrame
# TODO: Compute Q1, Q3, and IQR
# TODO: Calculate lower and upper bounds
# TODO: Find outlier indices and values
# TODO: Return result dictionary
pass