0
0
NumPydata~15 mins

Boolean indexing for filtering in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Boolean indexing for filtering
📖 Scenario: Imagine you have a list of daily temperatures recorded in a city. You want to find out which days were warm, meaning the temperature was above 20 degrees Celsius.
🎯 Goal: You will create a NumPy array of temperatures, set a threshold for warm days, use boolean indexing to filter the warm days, and then display those warm temperatures.
📋 What You'll Learn
Create a NumPy array called temperatures with the exact values: 15, 22, 19, 24, 18, 30, 21
Create a variable called warm_threshold and set it to 20
Use boolean indexing with temperatures and warm_threshold to create a new array called warm_days containing only temperatures above the threshold
Print the warm_days array
💡 Why This Matters
🌍 Real World
Filtering data based on conditions is common in weather analysis, finance, and health monitoring to focus on important values.
💼 Career
Data scientists often use boolean indexing to quickly select and analyze subsets of data without loops, making their code faster and cleaner.
Progress0 / 4 steps
1
Create the temperatures array
Import NumPy as np and create a NumPy array called temperatures with these exact values: 15, 22, 19, 24, 18, 30, 21
NumPy
Need a hint?

Use np.array([...]) to create the array with the exact numbers.

2
Set the warm day threshold
Create a variable called warm_threshold and set it to the number 20
NumPy
Need a hint?

Just assign the number 20 to the variable warm_threshold.

3
Filter warm days using boolean indexing
Use boolean indexing with temperatures and warm_threshold to create a new NumPy array called warm_days that contains only the temperatures greater than warm_threshold
NumPy
Need a hint?

Use temperatures > warm_threshold inside the square brackets to filter.

4
Print the warm days
Print the warm_days array to display the temperatures above the threshold
NumPy
Need a hint?

Use print(warm_days) to show the filtered temperatures.