Bird
Raised Fist0
NumPydata~5 mins

Why set operations matter in NumPy

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
Introduction

Set operations help us find common or different items between groups of data easily. They make comparing lists or arrays simple and fast.

Finding common customers between two sales lists
Identifying unique products sold in different stores
Removing duplicate entries from a dataset
Checking which students attended both classes
Comparing survey answers from two groups
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
This finds numbers present in both arrays.
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)
This combines all unique numbers from both arrays.
NumPy
union = np.union1d(arr1, arr2)
print(union)
This finds numbers in the first array but not in the second.
NumPy
diff = np.setdiff1d(arr1, arr2)
print(diff)
This finds numbers in either array but not in both.
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)
OutputSuccess
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/5)
1. What is the main purpose of using set operations in NumPy arrays?
easy
A. To multiply elements of arrays
B. To sort arrays in ascending order
C. To reshape arrays into different dimensions
D. To find common or unique elements between arrays

Solution

  1. Step 1: Understand set operations

    Set operations are used to compare arrays to find common or unique elements.
  2. Step 2: Identify the main purpose

    Sorting, multiplication, and reshaping are different array operations, not set operations.
  3. Final Answer:

    To find common or unique elements between arrays -> Option D
  4. Quick 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
A. np.intersect1d()
B. np.concatenate()
C. np.setdiff1d()
D. np.union1d()

Solution

  1. Step 1: Recall function names for set operations

    np.intersect1d() finds common elements between arrays.
  2. Step 2: Differentiate from other functions

    np.union1d() finds all unique elements combined, np.setdiff1d() finds differences, np.concatenate() joins arrays without set logic.
  3. Final Answer:

    np.intersect1d() -> Option A
  4. Quick 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
A. [1 2]
B. [3 4]
C. [5 6]
D. [1 2 3 4 5 6]

Solution

  1. Step 1: Understand np.setdiff1d()

    This function returns elements in the first array not in the second.
  2. Step 2: Apply to given arrays

    Elements in arr1 but not in arr2 are 1 and 2.
  3. Final Answer:

    [1 2] -> Option A
  4. Quick 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
A. arr2 is not a NumPy array
B. There is no error; code runs fine
C. np.intersect1d() cannot handle integers
D. np.intersect1d() requires both inputs to be lists

Solution

  1. Step 1: Check input types for np.intersect1d()

    np.intersect1d() accepts array-like inputs, including lists.
  2. Step 2: Verify code behavior

    arr2 is a list, which is valid input; code runs without error and outputs common elements.
  3. Final Answer:

    There is no error; code runs fine -> Option B
  4. Quick 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:
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
A. Use np.intersect1d(arr1, arr2)
B. Use np.union1d(arr1, arr2)
C. Use np.setxor1d(arr1, arr2)
D. Use np.setdiff1d(arr1, arr2)

Solution

  1. Step 1: Understand the problem

    We want elements unique to each array, not shared by both.
  2. Step 2: Identify correct function

    np.setxor1d() returns elements in either array but not in both (exclusive or).
  3. Final Answer:

    Use np.setxor1d(arr1, arr2) -> Option C
  4. Quick 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