Bird
Raised Fist0
NumPydata~20 mins

Why set operations matter in NumPy - Challenge Your Understanding

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
Challenge - 5 Problems
🎖️
Set Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of numpy set intersection
What is the output of this code that finds common elements between two numpy arrays?
NumPy
import numpy as np
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([4, 5, 6, 7])
result = np.intersect1d(arr1, arr2)
print(result)
A[4 5]
B[1 2 3]
C[6 7]
D[1 2 3 4 5 6 7]
Attempts:
2 left
💡 Hint
Look for elements that appear in both arrays.
❓ data_output
intermediate
2:00remaining
Number of unique elements after union
How many unique elements are in the union of these two numpy arrays?
NumPy
import numpy as np
arr1 = np.array([10, 20, 30, 40])
arr2 = np.array([30, 40, 50, 60])
union = np.union1d(arr1, arr2)
print(len(union))
A8
B4
C6
D5
Attempts:
2 left
💡 Hint
Count all unique elements from both arrays combined.
🔧 Debug
advanced
2:00remaining
Identify the error in numpy set difference usage
What error does this code raise when trying to find elements in arr1 not in arr2?
NumPy
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 3, 4])
result = np.setdiff(arr1, arr2)
print(result)
AAttributeError: module 'numpy' has no attribute 'setdiff'
B[1]
CTypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'
D[]
Attempts:
2 left
💡 Hint
Check if the function name is correct in numpy.
🚀 Application
advanced
2:00remaining
Using numpy set operations to find unique elements in one array
Which code snippet correctly finds elements in arr1 that are NOT in arr2 using numpy?
Anp.intersect1d(arr1, arr2)
Bnp.setdiff1d(arr1, arr2)
Cnp.union1d(arr1, arr2)
Dnp.unique(arr1 + arr2)
Attempts:
2 left
💡 Hint
Look for the function that returns elements in first array but not in second.
🧠 Conceptual
expert
2:00remaining
Why set operations improve data science workflows
Which statement best explains why set operations are important in data science?
AThey replace the need for statistical tests in data analysis.
BThey allow direct modification of original datasets without copying data.
CThey automatically visualize data relationships without extra code.
DThey help efficiently find common, unique, or different data points between datasets, enabling cleaner and faster analysis.
Attempts:
2 left
💡 Hint
Think about how comparing data sets helps in cleaning and analysis.

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