Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Creating boolean arrays
📖 Scenario: Imagine you have a list of temperatures recorded every day for a week. You want to find out which days were warmer than 20 degrees Celsius.
🎯 Goal: You will create a boolean array that shows True for days warmer than 20 degrees and False otherwise.
📋 What You'll Learn
Use numpy to create arrays
Create a numpy array with exact temperature values
Create a boolean array using a condition
Print the boolean array as output
💡 Why This Matters
🌍 Real World
This technique helps in filtering data based on conditions, like finding days warmer than a certain temperature.
💼 Career
Data scientists often create boolean arrays to filter and analyze data quickly and efficiently.
Progress0 / 4 steps
1
Create the temperature array
Create a numpy array called temperatures with these exact values: [18, 22, 19, 24, 20, 21, 17].
NumPy
Hint
Use np.array([...]) to create the array with the exact numbers.
2
Set the temperature threshold
Create a variable called threshold and set it to 20.
NumPy
Hint
Just assign the number 20 to a variable named threshold.
3
Create the boolean array
Create a boolean numpy array called warm_days that is True where temperatures are greater than threshold, and False otherwise.
NumPy
Hint
Use the comparison operator > between the array and the threshold variable.
4
Print the boolean array
Print the warm_days array to see which days were warmer than 20 degrees.
NumPy
Hint
Use print(warm_days) to display the boolean array.
Practice
(1/5)
1. What does a boolean array in numpy represent?
easy
A. An array of integers only
B. An array of True/False values based on a condition
C. An array of strings
D. An array with only zeros
Solution
Step 1: Understand boolean arrays
A boolean array stores True or False for each element, usually from a condition.
Step 2: Identify the correct description
The description 'An array of True/False values based on a condition' matches what a boolean array represents. Others refer to integers, strings, or zeros.
Final Answer:
An array of True/False values based on a condition -> Option B
Quick Check:
Boolean array = True/False values [OK]
Hint: Boolean arrays always hold True or False values [OK]
Common Mistakes:
Thinking boolean arrays hold numbers or strings
Confusing boolean arrays with integer arrays
2. Which of the following is the correct way to create a boolean array from a numpy array arr to check where elements are greater than 5?
easy
A. bool_arr = arr > 5
B. bool_arr = arr == 5
C. bool_arr = arr >= 5
D. bool_arr = arr < 5
Solution
Step 1: Understand the condition for boolean array
We want True where elements are greater than 5, so the condition is arr > 5.
Step 2: Match the correct syntax
bool_arr = arr > 5 uses arr > 5, which is correct. arr >= 5 checks greater than or equal to 5, arr == 5 checks equality to 5, and arr < 5 checks less than 5.
Final Answer:
bool_arr = arr > 5 -> Option A
Quick Check:
Condition for > 5 is arr > 5 [OK]
Hint: Use comparison operators directly on arrays for boolean arrays [OK]