Logical operations (and, or, not) in NumPy - Time & Space Complexity
We want to understand how the time it takes to do logical operations with numpy arrays changes as the arrays get bigger.
Specifically, how does the work grow when using and, or, and not on arrays?
Analyze the time complexity of the following code snippet.
import numpy as np
arr1 = np.random.choice([True, False], size=1000)
arr2 = np.random.choice([True, False], size=1000)
result_and = np.logical_and(arr1, arr2)
result_or = np.logical_or(arr1, arr2)
result_not = np.logical_not(arr1)
This code creates two arrays of True/False values and applies logical AND, OR, and NOT element-wise.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Element-wise logical operations on arrays.
- How many times: Once for each element in the arrays (n times, where n is array length).
As the array size grows, the number of logical checks grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 logical checks |
| 100 | About 100 logical checks |
| 1000 | About 1000 logical checks |
Pattern observation: The work grows in a straight line with the input size.
Time Complexity: O(n)
This means the time to do logical operations grows directly with the number of elements.
[X] Wrong: "Logical operations on arrays are instant and don't depend on size."
[OK] Correct: Each element must be checked, so bigger arrays take more time.
Understanding how logical operations scale helps you write efficient code when working with large data sets.
"What if we used nested arrays (2D arrays) instead of 1D? How would the time complexity change?"
Practice
Solution
Step 1: Understand logical AND operation in NumPy
Logical AND combines two boolean arrays and returns True only where both are True.Step 2: Identify the correct NumPy function
numpy.logical_and is the function designed for element-wise logical AND on boolean arrays.Final Answer:
numpy.logical_and -> Option AQuick Check:
AND operation = numpy.logical_and [OK]
- Confusing bitwise_and with logical_and
- Using logical_or instead of logical_and
- Using and_ which is not a NumPy function
a?Solution
Step 1: Recall the correct function for logical NOT in NumPy
The function is numpy.logical_not(array) to invert boolean values element-wise.Step 2: Check syntax correctness
numpy.logical_not(a) uses correct function and parentheses syntax; others are invalid or incorrect.Final Answer:
numpy.logical_not(a) -> Option CQuick Check:
NOT operation = numpy.logical_not(array) [OK]
- Using numpy.not which does not exist
- Using square brackets instead of parentheses
- Trying to negate with not keyword on arrays
import numpy as np x = np.array([True, False, True]) y = np.array([False, False, True]) result = np.logical_or(x, y) print(result)
Solution
Step 1: Understand logical OR operation element-wise
logical_or returns True if either element in the pair is True.Step 2: Apply logical_or to each pair in arrays x and y
Pairs: (True, False) -> True, (False, False) -> False, (True, True) -> True.Final Answer:
[True False True] -> Option DQuick Check:
logical_or([True,False,True],[False,False,True]) = [True,False,True] [OK]
- Confusing logical_or with logical_and
- Misreading boolean values in arrays
- Assuming all False if one element is False
import numpy as np arr = np.array([True, False, True]) result = arr and np.array([False, True, False]) print(result)
Solution
Step 1: Understand Python 'and' operator with arrays
'and' is a Python keyword that does not work element-wise on arrays; it expects single boolean values.Step 2: Identify correct way to do element-wise logical AND
Use numpy.logical_and(arr1, arr2) for element-wise AND on arrays.Final Answer:
Using 'and' operator directly on NumPy arrays causes an error -> Option BQuick Check:
Python 'and' fails on arrays; use logical_and [OK]
- Using Python 'and' instead of numpy.logical_and
- Trying to use 'and' without parentheses
- Converting arrays to lists unnecessarily
a = np.array([1, 0, 3, 0, 5]) b = np.array([0, 2, 3, 4, 0])
Which code snippet correctly creates a boolean array that is True where
a is non-zero AND b is zero?Solution
Step 1: Translate the condition into logical expressions
We want True where a is not zero AND b is zero, so conditions are a != 0 and b == 0.Step 2: Use numpy.logical_and to combine conditions element-wise
np.logical_and(a != 0, b == 0) returns True only where both conditions hold.Final Answer:
np.logical_and(a != 0, b == 0) -> Option AQuick Check:
AND of (a != 0) and (b == 0) = np.logical_and(a != 0, b == 0) [OK]
- Using logical_or instead of logical_and
- Negating conditions incorrectly
- Checking for zeros instead of non-zeros
Ready to prove it?
Pass the test and this pattern moves unknown
