📖 Scenario: You are working with a small dataset of temperatures recorded over a week. You want to find out which days were warmer than 20 degrees Celsius.
🎯 Goal: Create a NumPy array of temperatures, set a threshold temperature, create a Boolean array showing which days are warmer than the threshold, and print the Boolean array.
📋 What You'll Learn
Create a NumPy array called temperatures with the exact values: 18, 22, 19, 24, 20, 21, 17
Create a variable called threshold and set it to 20
Create a Boolean NumPy array called warm_days that is True where temperatures is greater than threshold
Print the warm_days array
💡 Why This Matters
🌍 Real World
Checking temperature data to find days warmer than a certain value is common in weather analysis and climate studies.
💼 Career
Data scientists often use Boolean arrays to filter data quickly and make decisions based on conditions.
Progress0 / 4 steps
1
Create the temperatures 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 values.
2
Set the threshold temperature
Create a variable called threshold and set it to the integer value 20
NumPy
Hint
Just assign the number 20 to the variable threshold.
3
Create the Boolean array for warm days
Create a Boolean NumPy array called warm_days that is True where temperatures is greater than threshold using the expression temperatures > threshold
NumPy
Hint
Use the comparison operator > between temperatures and threshold to get a Boolean array.
4
Print the Boolean array
Print the warm_days array using print(warm_days)
NumPy
Hint
Use print(warm_days) to display the Boolean array.
Practice
(1/5)
1. What does the bool type in numpy represent?
easy
A. Values that are either True or False
B. Integer numbers only
C. Floating point numbers
D. Text strings
Solution
Step 1: Understand the boolean type meaning
The boolean type stores only two possible values: True or False.
Step 2: Compare with other types
Integer, float, and string types store numbers or text, not boolean values.
Final Answer:
Values that are either True or False -> Option A
Quick Check:
Boolean type = True/False [OK]
Hint: Boolean means True or False only [OK]
Common Mistakes:
Confusing boolean with integer or float
Thinking boolean stores text
Assuming boolean can store multiple values
2. Which of the following is the correct way to create a numpy boolean array?
easy
A. np.array([1, 0, 1], dtype=str)
B. np.array([True, False, True], dtype=bool)
C. np.array([True, False, True], dtype=int)
D. np.array(['True', 'False'], dtype=bool)
Solution
Step 1: Check dtype for boolean array
To create a boolean array, use dtype=bool with True/False values.
Step 2: Validate options
np.array([True, False, True], dtype=bool) uses correct dtype and values. np.array([1, 0, 1], dtype=str) uses string dtype, not boolean. np.array([True, False, True], dtype=int) uses int dtype. np.array(['True', 'False'], dtype=bool) uses strings 'True', 'False' which are not boolean values.
Final Answer:
np.array([True, False, True], dtype=bool) -> Option B