Concept Flow - np.unique() for unique values
Input Array
Sort Array
Identify Unique Values
Return Unique Values Array
np.unique() takes an array, sorts it, finds unique values, and returns them as a new array.
import numpy as np arr = np.array([3, 1, 2, 3, 2, 4]) unique_vals = np.unique(arr) print(unique_vals)
| Step | Action | Array State | Result |
|---|---|---|---|
| 1 | Input array created | [3, 1, 2, 3, 2, 4] | Original array |
| 2 | Sort array internally | [1, 2, 2, 3, 3, 4] | Sorted array for processing |
| 3 | Identify unique values | [1, 2, 3, 4] | Unique values extracted |
| 4 | Return unique array | [1, 2, 3, 4] | Output array with unique values |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| arr | [3, 1, 2, 3, 2, 4] | [3, 1, 2, 3, 2, 4] | [3, 1, 2, 3, 2, 4] | [3, 1, 2, 3, 2, 4] (unchanged) |
| unique_vals | N/A | N/A | [1, 2, 3, 4] | [1, 2, 3, 4] |
np.unique(array) - Returns sorted unique values from array - Does not change original array - Useful to find distinct elements - Output is always sorted - Can return indices or counts with options