Boolean arrays help us mark True or False values for data. They are useful to filter or select data easily.
Creating boolean arrays in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
import numpy as np # Create a boolean array from a condition array = np.array([1, 2, 3, 4, 5]) boolean_array = array > 3 # Create a boolean array directly bool_array = np.array([True, False, True])
You can create boolean arrays by comparing arrays with conditions like >, <, ==.
Boolean arrays have values True or False only.
import numpy as np # Empty array empty_array = np.array([]) boolean_empty = empty_array > 0 print(boolean_empty)
import numpy as np # Single element array single_element_array = np.array([10]) boolean_single = single_element_array == 10 print(boolean_single)
import numpy as np # Condition at the start and end array = np.array([5, 3, 7, 1, 5]) boolean_condition = (array == 5) print(boolean_condition)
This program creates a boolean array by checking which numbers are greater than 5. Then it uses this boolean array to select only those numbers from the original array.
import numpy as np # Create an array of numbers numbers = np.array([2, 4, 6, 8, 10]) print("Original array:", numbers) # Create a boolean array where numbers are greater than 5 greater_than_five = numbers > 5 print("Boolean array (numbers > 5):", greater_than_five) # Use boolean array to filter numbers filtered_numbers = numbers[greater_than_five] print("Filtered numbers (only > 5):", filtered_numbers)
Time complexity: Creating a boolean array is O(n), where n is the number of elements.
Space complexity: Boolean arrays use less memory than integer arrays but still proportional to n.
Common mistake: Forgetting that boolean arrays are masks and must be used to index the original array for filtering.
Use boolean arrays when you want to select or mark elements based on conditions. For simple checks, use direct comparisons.
Boolean arrays store True/False values for each element.
They are created by applying conditions to arrays.
Boolean arrays help filter or select data easily.
Practice
numpy represent?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 BQuick Check:
Boolean array = True/False values [OK]
- Thinking boolean arrays hold numbers or strings
- Confusing boolean arrays with integer arrays
arr to check where elements are greater than 5?Solution
Step 1: Understand the condition for boolean array
We want True where elements are greater than 5, so the condition isarr > 5.Step 2: Match the correct syntax
bool_arr = arr > 5usesarr > 5, which is correct.arr >= 5checks greater than or equal to 5,arr == 5checks equality to 5, andarr < 5checks less than 5.Final Answer:
bool_arr = arr > 5 -> Option AQuick Check:
Condition for > 5 isarr > 5[OK]
- Using == instead of > for greater than
- Confusing >= with >
- Using wrong comparison operator
import numpy as np arr = np.array([2, 7, 4, 9]) bool_arr = arr < 5 print(bool_arr)
Solution
Step 1: Evaluate the condition arr < 5 for each element
Elements: 2 < 5 is True, 7 < 5 is False, 4 < 5 is True, 9 < 5 is False.Step 2: Form the boolean array
The boolean array is [True, False, True, False].Final Answer:
[True False True False] -> Option AQuick Check:
Check each element < 5 = [True False True False] [OK]
- Mixing up True and False values
- Forgetting to use < operator correctly
import numpy as np arr = np.array([10, 5, 10, 3]) bool_arr = arr = 10 print(bool_arr)
Solution
Step 1: Check the comparison operator
The code usesarr = 10which assigns 10 to arr, not compares.Step 2: Correct operator for comparison
To compare elements to 10, usearr == 10to create a boolean array.Final Answer:
Using '=' instead of '==' for comparison -> Option CQuick Check:
Comparison needs '==' not '=' [OK]
- Using single '=' instead of '=='
- Confusing assignment with comparison
data = np.array([3, 8, 1, 6, 0]), how can you create a boolean array that is True for elements greater than 2 and less than 7?Solution
Step 1: Understand element-wise logical operations in numpy
Use bitwise operators & and | for element-wise AND/OR, not 'and' or 'or'.Step 2: Apply correct syntax for combined condition
bool_arr = (data > 2) & (data < 7)uses(data > 2) & (data < 7), which is correct for element-wise AND.Final Answer:
bool_arr = (data > 2) & (data < 7) -> Option DQuick Check:
Use & for element-wise AND in numpy [OK]
- Using 'and' instead of '&' for element-wise AND
- Using '&&' which is invalid in Python
- Using 'or' instead of '&' for AND condition
