Boolean Indexing Practice Problem
This data science coding problem helps you practice Indexing, Slicing & Filtering, boolean indexing, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Indexing, Slicing & Filtering.
- Problem ID: 50
- Problem key: 50-boolean-indexing
- URL: https://datacrack.app/solve/50-boolean-indexing
- Difficulty: easy
- Topic: Indexing, Slicing & Filtering
- Module: NumPy Foundations
Problem Statement
# 🧩 Boolean Indexing
---
### 🎯 Goal
Boolean indexing (also called **boolean masking** or **fancy boolean selection**) is the most important NumPy selection technique. It's how `df[df['col'] > 0]` works in Pandas — it creates a boolean array and uses it to filter another array.
---
### 🔍 How It Works
```python
arr = np.array([1, -2, 3, -4, 5])
mask = arr > 0 # → [True, False, True, False, True]
arr[mask] # → [1, 3, 5]
# Or inline:
arr[arr > 0] # → [1, 3, 5]
```
The mask is a boolean array of the same shape. `arr[mask]` returns only elements where mask is `True`.
---
### 💻 Task
Implement `filter_above(data, threshold)` using NumPy boolean indexing.
---
### 📥 Input
- `data`: list of numbers
- `threshold`: number — filter for elements **strictly greater than** threshold
### 📤 Output
- A list of elements from `data` that are greater than `threshold`
---
### 🧩 Starter Code
```python
import numpy as np
def filter_above(data, threshold):
"""
Return elements strictly greater than threshold using boolean indexing.
Args:
data (list): Input numbers
threshold (float): Filter threshold
Returns:
list: Elements > threshold
"""
arr = np.array(data)
# 🧠 TODO
pass
```
---
### 💡 Example
```python
filter_above([1, -2, 3, -4, 5, -6, 7], 0)
# Expected: [1, 3, 5, 7]
```
---
### 🔑 Key Concepts
- Boolean indexing returns elements in **original order**
- Combines comparison (`>`) with selection (`[]`) in one step
- This is the exact mechanism behind `df[df['price'] > 100]`Starter Code
import numpy as np
def filter_above(data, threshold):
"""
Return elements strictly greater than threshold using boolean indexing.
Args:
data (list): Input numbers
threshold (float): Filter threshold
Returns:
list: Elements > threshold
"""
arr = np.array(data)
# 🧠 TODO
pass