0
0
NumPydata~10 mins

np.union1d() for union in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.union1d() for union
Start with two arrays
Find unique elements in each array
Combine unique elements
Sort combined elements
Return sorted union array
np.union1d() takes two arrays, finds unique elements in each, combines them, sorts the result, and returns the sorted union.
Execution Sample
NumPy
import numpy as np
arr1 = np.array([1, 3, 5])
arr2 = np.array([3, 4, 5])
result = np.union1d(arr1, arr2)
print(result)
This code finds the sorted union of two arrays arr1 and arr2.
Execution Table
StepActionarr1 uniquearr2 uniqueCombinedSorted UnionOutput
1Start with arr1 and arr2[1, 3, 5][3, 4, 5]
2Find unique elements in arr1[1, 3, 5]
3Find unique elements in arr2[3, 4, 5]
4Combine unique elements[1, 3, 5, 3, 4, 5]
5Sort combined elements[1, 3, 3, 4, 5, 5]
6Remove duplicates and output[1 3 4 5]
7EndExecution stops
💡 All unique elements combined and sorted; duplicates removed; union returned.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
arr1[1, 3, 5][1, 3, 5][1, 3, 5][1, 3, 5][1, 3, 5][1, 3, 5]
arr2[3, 4, 5][3, 4, 5][3, 4, 5][3, 4, 5][3, 4, 5][3, 4, 5]
combined[1, 3, 5, 3, 4, 5][1, 3, 3, 4, 5, 5][1, 3, 4, 5]
result[1 3 4 5]
Key Moments - 3 Insights
Why does the output not include duplicates like 3 and 5 twice?
np.union1d() automatically removes duplicates after combining arrays, as shown in step 6 of the execution_table where duplicates are removed before output.
Is the output always sorted?
Yes, np.union1d() sorts the combined unique elements before returning, as seen in step 5 where the combined array is sorted.
What happens if arrays have no common elements?
The function still combines and sorts all unique elements from both arrays, returning their union without duplicates, similar to the process in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the sorted union array before duplicates are removed?
A[1, 3, 4, 5]
B[1, 3, 3, 4, 5, 5]
C[1, 3, 5]
D[3, 4, 5]
💡 Hint
Check the 'Sorted Union' column at step 5 in the execution_table.
At which step does np.union1d() remove duplicates?
AStep 6
BStep 4
CStep 5
DStep 3
💡 Hint
Look at the 'Action' column in execution_table where duplicates are removed before output.
If arr2 was [6, 7, 8], how would the final output change?
A[6 7 8]
B[1 3 4 5]
C[1 3 5 6 7 8]
D[1 3 5]
💡 Hint
Refer to variable_tracker and think how combining unique elements from arr1 and arr2 changes.
Concept Snapshot
np.union1d(arr1, arr2)
- Combines two arrays
- Finds unique elements in both
- Sorts the combined unique elements
- Returns sorted union array
- Removes duplicates automatically
Full Transcript
np.union1d() takes two arrays and finds their union. It first finds unique elements in each array, then combines them. After combining, it sorts the elements and removes duplicates. The final output is a sorted array containing all unique elements from both input arrays. For example, with arr1=[1,3,5] and arr2=[3,4,5], the union is [1,3,4,5]. This process ensures no duplicates and sorted order in the result.