0
0
NumPydata~15 mins

np.union1d() for union in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Using np.union1d() to Find the Union of Two Arrays
📖 Scenario: Imagine you have two lists of product IDs from two different stores. You want to find all unique product IDs available in either store combined.
🎯 Goal: Build a small program that uses np.union1d() to find the union of two numpy arrays representing product IDs.
📋 What You'll Learn
Create two numpy arrays with exact product ID values
Create a variable to hold the union result
Use np.union1d() to find the union of the two arrays
Print the union array
💡 Why This Matters
🌍 Real World
Finding the combined list of unique items from multiple sources is common in inventory management, customer data merging, and survey analysis.
💼 Career
Data scientists often need to combine datasets and find unique entries efficiently using numpy functions like <code>np.union1d()</code>.
Progress0 / 4 steps
1
Create two numpy arrays with product IDs
Import numpy as np and create two numpy arrays called store1_products and store2_products with these exact values: [101, 102, 103, 104] and [103, 104, 105, 106] respectively.
NumPy
Need a hint?

Use np.array() to create numpy arrays with the exact product IDs.

2
Create a variable to hold the union result
Create a variable called all_products that will store the union of store1_products and store2_products.
NumPy
Need a hint?

Just create the variable all_products and set it to None for now.

3
Use np.union1d() to find the union of the two arrays
Assign to all_products the result of np.union1d(store1_products, store2_products) to find the union of the two arrays.
NumPy
Need a hint?

Use np.union1d() with the two arrays as arguments and assign the result to all_products.

4
Print the union array
Print the variable all_products to display the union of product IDs.
NumPy
Need a hint?

Use print(all_products) to show the union array.