0
0
NumPydata~10 mins

Combining conditions in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Combining conditions
Start with array
Apply condition 1
Apply condition 2
Combine conditions with & or |
Use combined condition to filter array
Get filtered result
We start with a data array, apply two conditions, combine them using logical AND (&) or OR (|), then filter the array based on the combined condition.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
cond = (arr > 2) & (arr < 5)
result = arr[cond]
print(result)
This code filters the array to keep only values greater than 2 and less than 5.
Execution Table
StepExpressionEvaluationResult
1arr = np.array([1, 2, 3, 4, 5])Create array[1 2 3 4 5]
2arr > 2Check each element > 2[False False True True True]
3arr < 5Check each element < 5[True True True True False]
4(arr > 2) & (arr < 5)Combine with AND[False False True True False]
5arr[cond]Filter array with combined condition[3 4]
6print(result)Output filtered array[3 4]
💡 All steps complete, filtered array contains elements satisfying both conditions.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
arrundefined[1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5]
condundefinedundefinedundefined[False False True True False][False False True True False][False False True True False]
resultundefinedundefinedundefinedundefined[3 4][3 4]
Key Moments - 2 Insights
Why do we use parentheses around each condition before combining them?
Parentheses ensure each condition is evaluated first before combining with & or |. Without them, Python's operator precedence can cause errors or unexpected results, as shown in step 4 of the execution_table.
What happens if we use 'and' or 'or' instead of '&' or '|' for combining conditions?
'and' and 'or' do not work element-wise on numpy arrays and will raise an error. We must use '&' for AND and '|' for OR to combine boolean arrays element-wise, as in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the combined condition array?
A[False True False True False]
B[True True True True True]
C[False False True True False]
D[True False True False True]
💡 Hint
Check the 'Result' column at step 4 in the execution_table.
At which step do we filter the original array using the combined condition?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Look for the step where 'arr[cond]' is used in the execution_table.
If we change the condition to (arr > 3) | (arr == 2), what will the filtered result be?
A[3 4 5]
B[2 4 5]
C[2 3 4]
D[1 2 3]
💡 Hint
Use the logic of OR (|) to combine conditions and check which elements satisfy either condition.
Concept Snapshot
Combining conditions in numpy:
- Use parentheses around each condition.
- Use & for AND, | for OR (element-wise).
- Combine boolean arrays to filter data.
- Example: arr[(arr > 2) & (arr < 5)] filters values between 2 and 5.
- Avoid 'and'/'or' with numpy arrays.
Full Transcript
This lesson shows how to combine conditions in numpy arrays to filter data. We start with an array, create two conditions, and combine them using & (AND) or | (OR). Parentheses are important to group conditions correctly. The combined boolean array is then used to select elements from the original array. We traced each step: creating the array, evaluating each condition, combining them, and filtering the array. Key points include using & and | for element-wise logical operations and avoiding Python's 'and'/'or' with arrays. The filtered result contains only elements that satisfy the combined condition.