Challenge - 5 Problems
Set Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Look for elements that appear in both arrays.
✗ Incorrect
np.intersect1d returns sorted unique values that are in both input arrays.
❓ data_output
intermediate2: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))
Attempts:
2 left
💡 Hint
Count all unique elements from both arrays combined.
✗ Incorrect
np.union1d returns sorted unique elements from both arrays combined. Here, elements are 10,20,30,40,50,60 totaling 6.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check if the function name is correct in numpy.
✗ Incorrect
The function np.setdiff does not exist. The correct function is np.setdiff1d.
🚀 Application
advanced2: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?
Attempts:
2 left
💡 Hint
Look for the function that returns elements in first array but not in second.
✗ Incorrect
np.setdiff1d returns sorted unique values in arr1 that are not in arr2.
🧠 Conceptual
expert2:00remaining
Why set operations improve data science workflows
Which statement best explains why set operations are important in data science?
Attempts:
2 left
💡 Hint
Think about how comparing data sets helps in cleaning and analysis.
✗ Incorrect
Set operations let you quickly find overlaps or differences between data sets, which is key for cleaning and combining data.