Bird
Raised Fist0
NumPydata~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the np.in1d() function do in NumPy?
easy
A. Finds the unique elements in an array.
B. Sorts the elements of an array in ascending order.
C. Calculates the sum of elements in an array.
D. Checks if elements of one array are present in another array and returns a boolean array.

Solution

  1. Step 1: Understand the purpose of np.in1d()

    The function checks membership of each element in the first array against the second array.
  2. Step 2: Identify the output type

    It returns a boolean array indicating True where elements are found and False otherwise.
  3. Final Answer:

    Checks if elements of one array are present in another array and returns a boolean array. -> Option D
  4. Quick Check:

    Membership test = Checks if elements of one array are present in another array and returns a boolean array. [OK]
Hint: Remember: np.in1d returns booleans for membership [OK]
Common Mistakes:
  • Confusing np.in1d() with sorting or summing functions
  • Expecting np.in1d() to return the matching elements instead of booleans
  • Thinking np.in1d() modifies the original arrays
2. Which of the following is the correct syntax to check if elements of array a are in array b using np.in1d()?
easy
A. np.in1d(b, a)
B. np.in1d(a, b)
C. np.in1d(a == b)
D. np.in1d(a, b, axis=1)

Solution

  1. Step 1: Recall np.in1d() parameter order

    The first argument is the array to test membership for, the second is the array to check against.
  2. Step 2: Evaluate each option

    np.in1d(a, b) uses correct order: np.in1d(a, b). np.in1d(b, a) reverses arrays, np.in1d(a == b) uses invalid syntax, np.in1d(a, b, axis=1) uses unsupported axis parameter.
  3. Final Answer:

    np.in1d(a, b) -> Option B
  4. Quick Check:

    Correct syntax = np.in1d(a, b) [OK]
Hint: First array is tested, second array is reference [OK]
Common Mistakes:
  • Swapping the order of arrays in np.in1d()
  • Adding unsupported parameters like axis
  • Using comparison operators inside np.in1d()
3. What is the output of the following code?
import numpy as np
x = np.array([1, 3, 5, 7])
y = np.array([3, 4, 5])
result = np.in1d(x, y)
print(result)
medium
A. [False True True False]
B. [True False True False]
C. [False True False False]
D. [True True True True]

Solution

  1. Step 1: Check each element of x against y

    1 in y? No (False), 3 in y? Yes (True), 5 in y? Yes (True), 7 in y? No (False).
  2. Step 2: Form the boolean array

    Result is [False, True, True, False].
  3. Final Answer:

    [False True True False] -> Option A
  4. Quick Check:

    Membership booleans = [False True True False] [OK]
Hint: Check each element one by one for membership [OK]
Common Mistakes:
  • Mixing up True and False positions
  • Assuming np.in1d returns matching elements instead of booleans
  • Forgetting to import numpy
4. The following code throws an error. What is the mistake?
import numpy as np
x = [1, 2, 3]
y = np.array([2, 3, 4])
result = np.in1d(x, y, axis=0)
print(result)
medium
A. np.in1d() requires both inputs to be lists.
B. x should be converted to a NumPy array before using np.in1d().
C. np.in1d() does not accept the 'axis' parameter.
D. The arrays x and y must have the same shape.

Solution

  1. Step 1: Check np.in1d() parameters

    np.in1d() accepts only two main parameters: the test array and the array to check against. It does not support an 'axis' parameter.
  2. Step 2: Identify the error cause

    Passing axis=0 causes a TypeError because it's not a valid argument.
  3. Final Answer:

    np.in1d() does not accept the 'axis' parameter. -> Option C
  4. Quick Check:

    Invalid parameter = np.in1d() does not accept the 'axis' parameter. [OK]
Hint: np.in1d() only takes two main arguments [OK]
Common Mistakes:
  • Trying to use axis parameter with np.in1d()
  • Assuming input types must match exactly
  • Thinking np.in1d() requires both inputs as arrays
5. You have two arrays:
data = np.array([10, 20, 30, 40, 50])
filter_vals = np.array([20, 40, 60])

You want to create a new array containing only elements from data that are present in filter_vals. Which code snippet correctly achieves this?
hard
A. filtered = data[np.in1d(data, filter_vals)]
B. filtered = filter_vals[np.in1d(filter_vals, data)]
C. filtered = np.in1d(data, filter_vals)
D. filtered = data[filter_vals]

Solution

  1. Step 1: Use np.in1d() to get boolean mask

    np.in1d(data, filter_vals) returns a boolean array marking elements of data present in filter_vals.
  2. Step 2: Use boolean mask to filter data

    Indexing data with this boolean mask selects only matching elements.
  3. Final Answer:

    filtered = data[np.in1d(data, filter_vals)] -> Option A
  4. Quick Check:

    Boolean mask indexing = filtered = data[np.in1d(data, filter_vals)] [OK]
Hint: Use np.in1d() mask to index original array [OK]
Common Mistakes:
  • Indexing filter_vals instead of data
  • Using np.in1d() without indexing
  • Trying to index with filter_vals directly