0
0
NumPydata~15 mins

np.in1d() for membership testing in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use boolean indexing: shipment[in_inventory] to get matching products.