0
0
NumPydata~10 mins

np.intersect1d() for intersection in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.intersect1d() for intersection
Input: Array1, Array2
Find common elements
Sort and remove duplicates
Return intersection array
The function takes two arrays, finds elements present in both, removes duplicates, sorts them, and returns the result.
Execution Sample
NumPy
import numpy as np
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])
result = np.intersect1d(arr1, arr2)
print(result)
This code finds the common elements between arr1 and arr2 and prints the sorted intersection without duplicates.
Execution Table
StepActionInput ArraysIntermediate ResultOutput
1Receive input arrays[1, 2, 3, 4], [3, 4, 5, 6]N/AN/A
2Find common elementsSame[3 4]N/A
3Sort and remove duplicates[3 4][3 4]N/A
4Return intersection arrayN/AN/A[3 4]
5Print resultN/AN/A[3 4]
💡 All common elements found, duplicates removed, sorted, and returned.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
arr1[1 2 3 4][1 2 3 4][1 2 3 4][1 2 3 4]
arr2[3 4 5 6][3 4 5 6][3 4 5 6][3 4 5 6]
resultN/A[3 4][3 4][3 4]
Key Moments - 2 Insights
Why does the output not include duplicates even if input arrays have duplicates?
np.intersect1d automatically removes duplicates from the intersection result, as shown in step 3 of the execution_table where duplicates are removed before returning.
Are the elements in the output always sorted?
Yes, np.intersect1d returns a sorted array of common elements, confirmed in step 3 where sorting happens before output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the intermediate result?
A[3 4]
B[1, 2]
C[5, 6]
D[2, 3]
💡 Hint
Check the 'Intermediate Result' column at step 2 in execution_table.
At which step does np.intersect1d remove duplicates and sort the result?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column describing sorting and duplicate removal.
If arr1 was [1, 2, 2, 3] and arr2 was [2, 2, 4], what would the final result be?
A[2, 2]
B[2]
C[1, 2]
D[1, 2, 4]
💡 Hint
np.intersect1d removes duplicates in the output, see variable_tracker for 'result'.
Concept Snapshot
np.intersect1d(arr1, arr2)
- Finds common elements in two arrays
- Removes duplicates automatically
- Returns sorted intersection array
- Useful for comparing datasets
- Output is always 1D sorted array
Full Transcript
This visual execution traces np.intersect1d which finds common elements between two numpy arrays. First, it takes two input arrays. Then it finds elements present in both arrays. Next, it removes duplicates and sorts the common elements. Finally, it returns the sorted array of unique common elements. The example code shows arrays [1, 2, 3, 4] and [3, 4, 5, 6] intersecting to [3, 4]. Key points are that duplicates are removed and output is sorted. The variable tracker shows how the result variable changes from empty to the final intersection. The quizzes test understanding of intermediate results, sorting step, and duplicate removal behavior.