Comparison operations help us check if values are equal, bigger, or smaller. This is useful to find patterns or filter data.
Comparison operations in NumPy
Introduction
Syntax
NumPy
array1 == array2 array1 != array2 array1 > array2 array1 >= array2 array1 < array2 array1 <= array2
These operations compare elements of arrays element-wise.
They return a new array of True/False values showing the comparison result.
Examples
NumPy
import numpy as np arr = np.array([1, 2, 3, 4]) print(arr > 2)
NumPy
arr1 = np.array([1, 2, 3]) arr2 = np.array([3, 2, 1]) print(arr1 == arr2)
NumPy
arr = np.array([5, 10, 15]) print(arr <= 10)
Sample Program
This program checks which scores are greater than or equal to 60. It prints the original scores, a True/False array showing who passed, and then only the passing scores.
NumPy
import numpy as np scores = np.array([55, 70, 85, 40, 90]) passing_score = 60 passed = scores >= passing_score print("Scores:", scores) print("Passed:", passed) # Filter scores that passed passed_scores = scores[passed] print("Scores that passed:", passed_scores)
Important Notes
Comparison results can be used to filter or select data easily.
All arrays compared must have the same shape or be broadcastable.
Summary
Comparison operations compare arrays element-wise and return True/False arrays.
They help find or filter data based on conditions.
Common operators: ==, !=, >, >=, <, <=.
Practice
1. What does the numpy expression
arr == 5 return when arr is a numpy array?easy
Solution
Step 1: Understand element-wise comparison
The expressionarr == 5compares 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 BQuick Check:
Element-wise comparison = Boolean array [OK]
Hint: Remember == returns True/False array element-wise [OK]
Common Mistakes:
- Thinking it returns a count or sum
- Expecting a single True/False value
- Confusing == with assignment =
2. Which of the following is the correct syntax to check if elements in numpy array
arr are greater than 10?easy
Solution
Step 1: Identify correct comparison operator
The operator for 'greater than' is>, soarr > 10checks elements greater than 10.Step 2: Check other options
arr => 10is invalid syntax,arr >> 10is a bitwise shift, andarr >= 10checks greater or equal, not strictly greater.Final Answer:
arr > 10-> Option CQuick Check:
Greater than operator is > [OK]
Hint: Use > for greater than, >= for greater or equal [OK]
Common Mistakes:
- Using => instead of >
- Confusing bitwise >> with comparison
- Using >= when > is asked
3. What is the output of the following code?
import numpy as np arr = np.array([3, 7, 2, 9]) result = arr >= 5 print(result)
medium
Solution
Step 1: Compare each element with 5
Elements: 3, 7, 2, 9. Check if each is >= 5: 3>=5 is False, 7>=5 is True, 2>=5 is False, 9>=5 is True.Step 2: Form boolean array
The result is [False, True, False, True].Final Answer:
[False True False True] -> Option DQuick Check:
Element-wise >= 5 = [False True False True] [OK]
Hint: Check each element against 5 carefully [OK]
Common Mistakes:
- Mixing up >= with >
- Misreading element values
- Expecting integer output instead of boolean
4. Identify the error in this numpy comparison code:
import numpy as np arr = np.array([1, 2, 3]) result = arr = 2 print(result)
medium
Solution
Step 1: Check comparison syntax
The code usesarr = 2which assigns 2 to arr, not compares. Comparison needs==.Step 2: Understand effect of assignment
Assignment overwrites arr with 2, so print(result) prints 2, not a boolean array.Final Answer:
Using single = instead of == for comparison -> Option AQuick Check:
Comparison needs ==, not = [OK]
Hint: Use == for comparison, = is assignment [OK]
Common Mistakes:
- Confusing = and ==
- Thinking print can't show arrays
- Believing arrays must convert to lists
5. Given two numpy arrays:
Which expression returns a boolean array showing where elements in
import numpy as np arr1 = np.array([1, 4, 6, 8]) arr2 = np.array([2, 4, 5, 10])
Which expression returns a boolean array showing where elements in
arr1 are less than or equal to elements in arr2?hard
Solution
Step 1: Understand element-wise comparison between arrays
Comparing two arrays with operators like <= compares each element pair at the same index.Step 2: Identify operator for 'less than or equal'
The operator <= checks if elements in arr1 are less than or equal to corresponding elements in arr2.Final Answer:
arr1 <= arr2 -> Option AQuick Check:
Use <= for less or equal element-wise [OK]
Hint: Use <= to compare arrays element-wise for less or equal [OK]
Common Mistakes:
- Using < instead of <= when equal counts
- Using == which only checks equality
- Using >= which is opposite
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
