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
Using np.in1d() for Membership Testing
📖 Scenario: You work in a store that tracks product IDs. You want to check which products from a new shipment are already in your current inventory.
🎯 Goal: Build a small program that uses np.in1d() to find which product IDs from the shipment are already in the inventory.
📋 What You'll Learn
Create two numpy arrays: one for current inventory product IDs and one for new shipment product IDs.
Create a variable to store the membership test result using np.in1d().
Print the boolean array showing which shipment products are in the inventory.
💡 Why This Matters
🌍 Real World
Stores and warehouses often need to check if new products are already in stock to avoid duplicates or manage inventory.
💼 Career
Data analysts and inventory managers use membership testing to clean and filter product data efficiently.
Progress0 / 4 steps
1
Create numpy arrays for inventory and shipment
Import numpy as np. Create a numpy array called inventory with these product IDs: 101, 102, 103, 104. Create another numpy array called shipment with these product IDs: 102, 105, 101, 107.
NumPy
Hint
Use np.array() to create arrays with the exact product IDs given.
2
Create a variable for membership test
Create a variable called in_inventory that uses np.in1d() to check which product IDs in shipment are also in inventory.
NumPy
Hint
Use np.in1d(shipment, inventory) to get a boolean array.
3
Print the membership result
Print the variable in_inventory to show which shipment products are in the inventory.
NumPy
Hint
Use print(in_inventory) to display the boolean array.
4
Filter shipment products that are in inventory
Create a variable called products_in_inventory that uses shipment[in_inventory] to get the product IDs from shipment that are in inventory. Then print products_in_inventory.
NumPy
Hint
Use boolean indexing: shipment[in_inventory] to get matching products.
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
Step 1: Understand the purpose of np.in1d()
The function checks membership of each element in the first array against the second array.
Step 2: Identify the output type
It returns a boolean array indicating True where elements are found and False otherwise.
Final Answer:
Checks if elements of one array are present in another array and returns a boolean array. -> Option D
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
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.
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.
Final Answer:
np.in1d(a, b) -> Option B
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
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).