What does the comparison operation arr1 > arr2 do in NumPy?
It compares each element of arr1 with the corresponding element of arr2 and returns a boolean array where each value is True if the element in arr1 is greater than the element in arr2, otherwise False.
Click to reveal answer
intermediate
How does NumPy handle comparison operations between arrays of different shapes?
NumPy uses broadcasting rules to compare arrays of different shapes. It stretches the smaller array along dimensions of size 1 to match the larger array's shape, then performs element-wise comparison.
Click to reveal answer
beginner
What is the output type of a comparison operation between two NumPy arrays?
The output is a NumPy array of boolean values (True or False) indicating the result of the comparison for each element.
Click to reveal answer
beginner
Which comparison operators can be used element-wise on NumPy arrays?
You can use <, <=, >, >=, ==, and != for element-wise comparisons on NumPy arrays.
Click to reveal answer
intermediate
How can you find if any element in a NumPy boolean array is True after a comparison?
Use the numpy.any() function on the boolean array. It returns True if at least one element is True, otherwise False.
Click to reveal answer
What does arr == 5 return if arr is a NumPy array?
AA boolean array showing where elements equal 5
BA single boolean value
CAn array of integers
DAn error
✗ Incorrect
The operation compares each element to 5 and returns a boolean array with True where elements equal 5.
Which operator checks if elements in one array are not equal to another array?
A==
B!=
C>=
D<
✗ Incorrect
The != operator returns True where elements differ between arrays.
If arr1 has shape (3,1) and arr2 has shape (3,4), what happens when you do arr1 < arr2?
AAn error occurs due to shape mismatch
Barr2 is reshaped to (3,1)
COnly first column is compared
DBroadcasting stretches arr1 to (3,4) and compares element-wise
✗ Incorrect
NumPy broadcasts arr1 to match arr2's shape and compares each element.
What type of array results from arr1 >= arr2?
AFloat array
BInteger array
CBoolean array
DString array
✗ Incorrect
Comparison operations produce boolean arrays indicating True or False per element.
How do you check if any element in a boolean NumPy array is True?
Anumpy.any()
Bnumpy.all()
Csum()
Dlen()
✗ Incorrect
numpy.any() returns True if at least one element is True.
Explain how element-wise comparison works between two NumPy arrays.
Think about how NumPy compares each element and what it returns.
You got /3 concepts.
Describe how to use comparison operators to filter data in a NumPy array.
Consider how True/False arrays help pick data.
You got /3 concepts.
Practice
(1/5)
1. What does the numpy expression arr == 5 return when arr is a numpy array?
easy
A. The sum of all elements equal to 5
B. An array of True/False values showing where elements equal 5
C. The count of elements equal to 5
D. A single True or False value
Solution
Step 1: Understand element-wise comparison
The expression arr == 5 compares each element of the array to 5 individually.
Step 2: Result type of comparison
This returns a new array of the same shape with True where the element equals 5, and False otherwise.
Final Answer:
An array of True/False values showing where elements equal 5 -> Option B