Set operations help us find common or different items between groups of data easily. They make comparing lists or arrays simple and fast.
Why set operations matter in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
numpy.intersect1d(array1, array2) numpy.union1d(array1, array2) numpy.setdiff1d(array1, array2) numpy.setxor1d(array1, array2)
These functions work on 1D arrays and return sorted unique values.
Use intersect1d for common items, union1d for all unique items combined, setdiff1d for items in first array not in second, and setxor1d for items in either array but not both.
Examples
NumPy
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([3, 4, 5, 6]) common = np.intersect1d(arr1, arr2) print(common)
NumPy
union = np.union1d(arr1, arr2)
print(union)NumPy
diff = np.setdiff1d(arr1, arr2)
print(diff)NumPy
xor = np.setxor1d(arr1, arr2)
print(xor)Sample Program
This program compares product IDs sold in two stores using set operations to find common, all, unique to one store, and exclusive products.
NumPy
import numpy as np # Two lists of product IDs sold in two stores store1 = np.array([101, 102, 103, 104, 105]) store2 = np.array([104, 105, 106, 107]) # Products sold in both stores common_products = np.intersect1d(store1, store2) print("Common products:", common_products) # All unique products sold all_products = np.union1d(store1, store2) print("All products:", all_products) # Products only in store1 only_store1 = np.setdiff1d(store1, store2) print("Only in store1:", only_store1) # Products sold in one store but not both exclusive_products = np.setxor1d(store1, store2) print("Exclusive products:", exclusive_products)
Important Notes
Set operations automatically remove duplicates and sort the results.
These operations are very useful for cleaning and comparing data quickly.
Summary
Set operations help compare and combine data easily.
They find common, unique, or different items between arrays.
NumPy provides simple functions to do these tasks fast.
Practice
1. What is the main purpose of using set operations in NumPy arrays?
easy
Solution
Step 1: Understand set operations
Set operations are used to compare arrays to find common or unique elements.Step 2: Identify the main purpose
Sorting, multiplication, and reshaping are different array operations, not set operations.Final Answer:
To find common or unique elements between arrays -> Option DQuick Check:
Set operations = find common/unique elements [OK]
Hint: Set operations = compare arrays for common or unique items [OK]
Common Mistakes:
- Confusing set operations with sorting or reshaping
- Thinking set operations multiply elements
- Assuming set operations change array shape
2. Which NumPy function is used to find the intersection of two arrays?
easy
Solution
Step 1: Recall function names for set operations
np.intersect1d() finds common elements between arrays.Step 2: Differentiate from other functions
np.union1d() finds all unique elements combined, np.setdiff1d() finds differences, np.concatenate() joins arrays without set logic.Final Answer:
np.intersect1d() -> Option AQuick Check:
Intersection = np.intersect1d() [OK]
Hint: Intersection means common elements, use np.intersect1d() [OK]
Common Mistakes:
- Using np.union1d() for intersection
- Confusing set difference with intersection
- Using np.concatenate() which just joins arrays
3. What is the output of the following code?
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([3, 4, 5, 6]) result = np.setdiff1d(arr1, arr2) print(result)
medium
Solution
Step 1: Understand np.setdiff1d()
This function returns elements in the first array not in the second.Step 2: Apply to given arrays
Elements in arr1 but not in arr2 are 1 and 2.Final Answer:
[1 2] -> Option AQuick Check:
Set difference arr1 - arr2 = [1 2] [OK]
Hint: Set difference = items in first array not in second [OK]
Common Mistakes:
- Confusing set difference with intersection
- Expecting union instead of difference
- Misreading which array is first
4. The following code throws an error. What is the problem?
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = [2, 3, 4] result = np.intersect1d(arr1, arr2) print(result)
medium
Solution
Step 1: Check input types for np.intersect1d()
np.intersect1d() accepts array-like inputs, including lists.Step 2: Verify code behavior
arr2 is a list, which is valid input; code runs without error and outputs common elements.Final Answer:
There is no error; code runs fine -> Option BQuick Check:
np.intersect1d() accepts lists and arrays [OK]
Hint: np.intersect1d() accepts lists or arrays as input [OK]
Common Mistakes:
- Assuming inputs must be NumPy arrays
- Thinking np.intersect1d() only works with arrays
- Expecting error due to mixed input types
5. You have two arrays:
How can you find all unique elements that appear in either array but not in both?
arr1 = np.array([1, 2, 2, 3, 4]) arr2 = np.array([2, 3, 5])
How can you find all unique elements that appear in either array but not in both?
hard
Solution
Step 1: Understand the problem
We want elements unique to each array, not shared by both.Step 2: Identify correct function
np.setxor1d() returns elements in either array but not in both (exclusive or).Final Answer:
Use np.setxor1d(arr1, arr2) -> Option CQuick Check:
Unique elements in either array = np.setxor1d() [OK]
Hint: Exclusive elements = np.setxor1d() finds unique non-shared items [OK]
Common Mistakes:
- Using union instead of exclusive or
- Using intersection which finds common elements
- Using set difference which is one-sided
