Bird
Raised Fist0
NumPydata~20 mins

Set operations on structured data in NumPy - Practice Problems & Coding Challenges

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 structured array intersection
What is the output of this code that finds the intersection of two structured numpy arrays based on all fields?
NumPy
import numpy as np

arr1 = np.array([(1, 'apple'), (2, 'banana'), (3, 'cherry')], dtype=[('id', 'i4'), ('fruit', 'U10')])
arr2 = np.array([(2, 'banana'), (3, 'cherry'), (4, 'date')], dtype=arr1.dtype)

result = np.intersect1d(arr1, arr2)
print(result)
A[(2, 'banana') (3, 'cherry')]
B[(1, 'apple') (2, 'banana') (3, 'cherry')]
C[(3, 'cherry') (4, 'date')]
D[(1, 'apple') (4, 'date')]
Attempts:
2 left
💡 Hint
Think about which records appear in both arrays exactly.
❓ data_output
intermediate
1:30remaining
Number of unique records after union
Given two structured numpy arrays, what is the number of unique records after performing a union operation?
NumPy
import numpy as np

arr1 = np.array([(10, 'red'), (20, 'blue')], dtype=[('code', 'i4'), ('color', 'U10')])
arr2 = np.array([(20, 'blue'), (30, 'green')], dtype=arr1.dtype)

union_result = np.union1d(arr1, arr2)
print(len(union_result))
A2
B3
C4
D1
Attempts:
2 left
💡 Hint
Count all unique records from both arrays combined.
🔧 Debug
advanced
2:00remaining
Error in set difference on structured arrays
What error does this code raise when trying to find the difference between two structured numpy arrays?
NumPy
import numpy as np

arr1 = np.array([(1, 2.0), (3, 4.0)], dtype=[('a', 'i4'), ('b', 'f4')])
arr2 = np.array([(3, 4.0)], dtype=[('a', 'i4'), ('b', 'f4')])

diff = np.setdiff1d(arr1, arr2, assume_unique=True)
print(diff)
ATypeError: unhashable type: 'numpy.void'
BValueError: operands could not be broadcast together
CNo error, outputs [(1, 2.0)]
DIndexError: index out of bounds
Attempts:
2 left
💡 Hint
Check if the operation is valid for structured arrays and what output is expected.
❓ visualization
advanced
2:30remaining
Visualizing set intersection of structured arrays
Which option correctly plots a Venn diagram showing the intersection size of two structured numpy arrays based on their 'id' field?
NumPy
import numpy as np
import matplotlib.pyplot as plt
from matplotlib_venn import venn2

arr1 = np.array([(1, 'x'), (2, 'y'), (3, 'z')], dtype=[('id', 'i4'), ('val', 'U1')])
arr2 = np.array([(2, 'y'), (3, 'z'), (4, 'w')], dtype=arr1.dtype)

ids1 = set(arr1['id'])
ids2 = set(arr2['id'])

plt.figure(figsize=(5,5))
A
venn2([list(arr1), list(arr2)], set_labels=('arr1', 'arr2'))
plt.show()
B
venn2([arr1, arr2], set_labels=('arr1', 'arr2'))
plt.show()
C
venn2([arr1['val'], arr2['val']], set_labels=('arr1', 'arr2'))
plt.show()
D
venn2([ids1, ids2], set_labels=('arr1', 'arr2'))
plt.show()
Attempts:
2 left
💡 Hint
Venn diagram requires sets of hashable elements, use the 'id' field.
🧠 Conceptual
expert
3:00remaining
Why does np.intersect1d require structured arrays to have the same dtype?
Why must two structured numpy arrays have the same dtype to use np.intersect1d correctly?
ABecause np.intersect1d compares raw bytes and different dtypes change memory layout, causing incorrect comparisons.
BBecause np.intersect1d only works on 1D numeric arrays, not structured arrays.
CBecause structured arrays with different dtypes are always empty when intersected, so dtype doesn't matter.
DBecause numpy automatically converts dtypes internally, so different dtypes cause slowdowns but no errors.
Attempts:
2 left
💡 Hint
Think about how numpy compares structured array elements at the memory level.

Practice

(1/5)
1. What does the numpy.intersect1d function do when applied to two structured arrays?
easy
A. Finds rows present only in the second array
B. Combines all rows from both arrays without duplicates
C. Finds the common rows present in both arrays
D. Finds rows present only in the first array

Solution

  1. Step 1: Understand intersect1d purpose

    numpy.intersect1d returns elements common to both input arrays.
  2. Step 2: Apply to structured arrays

    For structured arrays, it compares rows and returns those present in both arrays.
  3. Final Answer:

    Finds the common rows present in both arrays -> Option C
  4. Quick Check:

    Intersection = common rows [OK]
Hint: Intersect means common elements only [OK]
Common Mistakes:
  • Confusing intersect1d with union1d
  • Thinking it returns unique rows from one array only
  • Assuming it returns rows exclusive to one array
2. Which of the following is the correct syntax to find the union of two structured numpy arrays a and b?
easy
A. numpy.union(a | b)
B. numpy.union(a, b)
C. numpy.setunion(a, b)
D. numpy.union1d(a, b)

Solution

  1. Step 1: Recall numpy union function

    The correct function to find union is numpy.union1d.
  2. Step 2: Check syntax correctness

    The syntax is numpy.union1d(a, b) with two arguments.
  3. Final Answer:

    numpy.union1d(a, b) -> Option D
  4. Quick Check:

    Use union1d for union operation [OK]
Hint: Use union1d, not union or setunion [OK]
Common Mistakes:
  • Using nonexistent functions like union or setunion
  • Passing arguments incorrectly with bitwise operators
  • Confusing union1d with intersect1d
3. Given two structured arrays:
a = np.array([(1, 'A'), (2, 'B'), (3, 'C')], dtype=[('id', int), ('val', 'U1')])
b = np.array([(2, 'B'), (4, 'D')], dtype=[('id', int), ('val', 'U1')])
print(np.setdiff1d(a, b))

What is the output?
medium
A. [(1, 'A') (3, 'C')]
B. [(2, 'B') (4, 'D')]
C. [(1, 'A') (2, 'B') (3, 'C')]
D. [(4, 'D')]

Solution

  1. Step 1: Understand setdiff1d behavior

    np.setdiff1d(a, b) returns rows in a not in b.
  2. Step 2: Compare rows of a and b

    Rows (2, 'B') is common, so excluded. Remaining are (1, 'A') and (3, 'C').
  3. Final Answer:

    [(1, 'A') (3, 'C')] -> Option A
  4. Quick Check:

    Difference = rows only in a [OK]
Hint: Setdiff1d returns items only in first array [OK]
Common Mistakes:
  • Including common rows in output
  • Confusing setdiff1d with union1d or intersect1d
  • Expecting output from second array instead
4. Consider this code snippet:
a = np.array([(1, 'X'), (2, 'Y')], dtype=[('id', int), ('val', 'U1')])
b = np.array([(2, 'Y'), (3, 'Z')], dtype=[('id', int), ('val', 'U2')])
result = np.setxor1d(a, b)
print(result)

It raises an error. What is the likely cause?
medium
A. Arrays must be sorted before setxor1d
B. Structured arrays have different dtypes or field order
C. setxor1d does not support structured arrays
D. Missing import statement for numpy

Solution

  1. Step 1: Check dtype compatibility

    For set operations on structured arrays, dtypes and field order must match exactly.
  2. Step 2: Identify cause of error

    If dtypes differ or field order differs, setxor1d raises an error.
  3. Final Answer:

    Structured arrays have different dtypes or field order -> Option B
  4. Quick Check:

    Matching dtypes needed for set operations [OK]
Hint: Ensure structured arrays have identical dtypes [OK]
Common Mistakes:
  • Assuming setxor1d can't handle structured arrays
  • Forgetting to check dtype and field order
  • Thinking arrays must be sorted first
5. You have two structured arrays representing employee records:
emp1 = np.array([(101, 'Alice'), (102, 'Bob'), (103, 'Carol')], dtype=[('id', int), ('name', 'U10')])
emp2 = np.array([(102, 'Bob'), (104, 'Dave')], dtype=[('id', int), ('name', 'U10')])

You want to find employees who are in either list but not both (exclusive employees). Which numpy function and code will give the correct result?
hard
A. np.setxor1d(emp1, emp2)
B. np.union1d(emp1, emp2)
C. np.intersect1d(emp1, emp2)
D. np.setdiff1d(emp1, emp2)

Solution

  1. Step 1: Understand exclusive elements

    Exclusive employees are those in one array but not both, which is the symmetric difference.
  2. Step 2: Identify correct numpy function

    np.setxor1d returns elements in either array but not in both.
  3. Final Answer:

    np.setxor1d(emp1, emp2) -> Option A
  4. Quick Check:

    Symmetric difference = setxor1d [OK]
Hint: Use setxor1d for exclusive elements [OK]
Common Mistakes:
  • Using union1d which includes all elements
  • Using intersect1d which finds common only
  • Using setdiff1d which finds only one-sided difference