Comparison operations in NumPy - Time & Space Complexity
We want to understand how long it takes to compare values in numpy arrays as the array gets bigger.
How does the time needed grow when we check many numbers?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.random.randint(0, 100, size=1000)
result = arr > 50
This code creates an array of 1000 random numbers and compares each number to 50, producing a True/False array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing each element in the array to the number 50.
- How many times: Once for every element in the array, so 1000 times here.
When the array size grows, the number of comparisons grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The number of comparisons grows directly with the number of elements.
Time Complexity: O(n)
This means the time to compare grows in a straight line with the number of items.
[X] Wrong: "Comparing all elements is faster because numpy is optimized, so it's almost constant time."
[OK] Correct: Even though numpy is fast, it still checks each element once, so time grows with array size.
Knowing how comparison time grows helps you explain performance when working with big data arrays.
"What if we compare two arrays element-wise instead of one array to a number? How would the time complexity change?"
Practice
arr == 5 return when arr is a numpy array?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]
- Thinking it returns a count or sum
- Expecting a single True/False value
- Confusing == with assignment =
arr are greater than 10?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]
- Using => instead of >
- Confusing bitwise >> with comparison
- Using >= when > is asked
import numpy as np arr = np.array([3, 7, 2, 9]) result = arr >= 5 print(result)
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]
- Mixing up >= with >
- Misreading element values
- Expecting integer output instead of boolean
import numpy as np arr = np.array([1, 2, 3]) result = arr = 2 print(result)
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]
- Confusing = and ==
- Thinking print can't show arrays
- Believing arrays must convert to lists
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?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]
- Using < instead of <= when equal counts
- Using == which only checks equality
- Using >= which is opposite
Ready to prove it?
Pass the test and this pattern moves unknown
