0
0
NumPydata~20 mins

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

Choose your learning style9 modes available
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.