0
0
NumPydata~10 mins

np.in1d() for membership testing in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.in1d() for membership testing
Start with array A and array B
Check each element of A in B?
For each element in A
Return boolean array of membership
np.in1d() checks if each element of one array is present in another array and returns a boolean array showing membership.
Execution Sample
NumPy
import numpy as np
A = np.array([1, 2, 3, 4])
B = np.array([2, 4, 6])
result = np.in1d(A, B)
print(result)
This code tests which elements of A are in B and prints a boolean array.
Execution Table
StepElement from AIs in B?Boolean Output
11NoFalse
22YesTrue
33NoFalse
44YesTrue
5EndAll elements checkedResult array complete
💡 All elements of A checked for membership in B, output array created.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
A[1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4]
B[2, 4, 6][2, 4, 6][2, 4, 6][2, 4, 6][2, 4, 6][2, 4, 6]
result[][False][False, True][False, True, False][False, True, False, True][False, True, False, True]
Key Moments - 3 Insights
Why does np.in1d return a boolean array instead of the elements themselves?
np.in1d is designed to test membership and returns True or False for each element in the first array, as shown in the execution_table rows 1-4.
What happens if an element in A is not found in B?
The output for that element is False, as seen in execution_table steps 1 and 3 where elements 1 and 3 are not in B.
Does np.in1d change the original arrays A or B?
No, np.in1d only reads A and B and returns a new boolean array without modifying the originals, confirmed by variable_tracker showing A and B unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the boolean output for element 3 from A?
ATrue
BFalse
CNone
DError
💡 Hint
Check execution_table row 3 for element 3's membership result.
At which step does the condition 'element in B' become True for the first time?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at execution_table rows 1 and 2 to see when 'Is in B?' is Yes.
If B was changed to [1, 3, 5], what would be the boolean output for element 4 from A?
ATrue
BError
CFalse
DDepends on A
💡 Hint
Refer to how membership is checked in execution_table and variable_tracker.
Concept Snapshot
np.in1d(array1, array2) -> boolean array
Checks if each element of array1 is in array2.
Returns True for membership, False otherwise.
Does not modify input arrays.
Useful for filtering or masking data.
Full Transcript
This visual execution traces np.in1d(), a numpy function that tests membership of elements from one array in another. We start with two arrays, A and B. For each element in A, np.in1d checks if it is present in B. The result is a boolean array where each position corresponds to an element in A: True if found in B, False if not. The execution table shows each step checking elements 1, 2, 3, and 4 from A against B. The variable tracker confirms that A and B remain unchanged while the result array builds up. Key moments clarify why the output is boolean, what happens when elements are missing, and that inputs are not modified. The quiz tests understanding of specific steps and hypothetical changes. This function is handy for quickly finding which items belong to a set.