A Boolean type in NumPy represents values that can be either True or False. It is used to store truth values and is useful for filtering and conditional operations.
Click to reveal answer
beginner
How do you create a NumPy array of Boolean values?
You can create a Boolean NumPy array by passing a list of True and False values to np.array(), for example: np.array([True, False, True]).
Click to reveal answer
beginner
What is the data type name for Boolean arrays in NumPy?
The data type name for Boolean arrays in NumPy is 'bool' or 'bool_' which stores True or False values efficiently.
Click to reveal answer
intermediate
How can Boolean arrays be used in NumPy?
Boolean arrays can be used to filter data, perform element-wise logical operations, and control flow in data processing tasks.
Click to reveal answer
intermediate
What happens when you perform a logical operation between two Boolean NumPy arrays?
Performing logical operations like AND (&), OR (|), and NOT (~) between Boolean arrays results in a new Boolean array with element-wise operation results.
Click to reveal answer
Which NumPy data type stores Boolean values?
Abool
Bint
Cfloat
Dstr
✗ Incorrect
The 'bool' data type in NumPy stores Boolean values True or False.
What is the output type when you compare two NumPy arrays element-wise?
AInteger array
BString array
CFloat array
DBoolean array
✗ Incorrect
Element-wise comparison returns a Boolean array indicating True or False for each comparison.
Which operator is used for element-wise logical AND in NumPy Boolean arrays?
A&
B&&
Cand
D|
✗ Incorrect
The '&' operator performs element-wise logical AND on Boolean NumPy arrays.
How do you create a Boolean array from a condition in NumPy?
AUsing np.bool_()
BUsing a comparison like arr > 5
CUsing np.array([True, False])
DUsing np.int32()
✗ Incorrect
A condition like arr > 5 returns a Boolean array where each element shows if the condition is True or False.
What does the ~ operator do on a Boolean NumPy array?
ALogical AND
BLogical OR
CLogical NOT (negation)
DNo operation
✗ Incorrect
The '~' operator performs element-wise logical NOT, flipping True to False and vice versa.
Explain what a Boolean type is in NumPy and how it can be used in data analysis.
Think about how True/False values help select or filter data.
You got /4 concepts.
Describe how to create and manipulate Boolean arrays in NumPy with examples.
Consider how conditions and logical operators work on arrays.
You got /4 concepts.
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