Bird
Raised Fist0
NumPydata~10 mins

np.unique() for unique values in NumPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.
Execution Sample
NumPy
import numpy as np
arr = np.array([3, 1, 2, 3, 2, 4])
unique_vals = np.unique(arr)
print(unique_vals)
This code finds and prints the unique values from the given array.
Execution Table
StepActionArray StateResult
1Input array created[3, 1, 2, 3, 2, 4]Original array
2Sort array internally[1, 2, 2, 3, 3, 4]Sorted array for processing
3Identify unique values[1, 2, 3, 4]Unique values extracted
4Return unique array[1, 2, 3, 4]Output array with unique values
💡 All unique values identified and returned as a sorted array.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
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_valsN/AN/A[1, 2, 3, 4][1, 2, 3, 4]
Key Moments - 2 Insights
Why does np.unique() return a sorted array of unique values?
np.unique() sorts the array internally to easily identify unique values in order, as shown in step 2 and 3 of the execution_table.
Does np.unique() change the original array?
No, the original array 'arr' remains unchanged throughout, as shown in variable_tracker where 'arr' keeps its initial values.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the array state?
A[3, 1, 2, 3, 2, 4]
B[1, 2, 2, 3, 3, 4]
C[1, 2, 3, 4]
D[4, 3, 2, 1]
💡 Hint
Check the 'Array State' column at step 3 in the execution_table.
At which step does np.unique() identify unique values?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table to find when unique values are identified.
If the input array was already sorted, how would step 2 change?
AStep 2 would still sort but no change in array
BStep 2 would be skipped
CStep 2 would reverse the array
DStep 2 would remove duplicates
💡 Hint
np.unique() always sorts internally even if the array is sorted; see step 2 in execution_table.
Concept Snapshot
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
Full Transcript
np.unique() is a function in numpy that finds all unique values in an array. It first sorts the array internally, then identifies unique values by removing duplicates. The original array stays the same. The function returns a new array with unique values sorted in ascending order. This is useful when you want to know which distinct values exist in your data. The execution steps show the input array, sorting, unique identification, and final output. Variables track the original and unique arrays separately to avoid confusion.

Practice

(1/5)
1. What does the np.unique() function do in NumPy?
easy
A. Calculates the sum of all elements
B. Sorts the array in descending order
C. Reverses the order of elements
D. Finds all unique values in an array

Solution

  1. Step 1: Understand the purpose of np.unique()

    This function is designed to find all different (unique) values in a NumPy array or list.
  2. Step 2: Compare with other options

    Sorting, summing, or reversing are different operations and not what np.unique() does.
  3. Final Answer:

    Finds all unique values in an array -> Option D
  4. Quick Check:

    np.unique() = unique values [OK]
Hint: Remember: unique means different values only [OK]
Common Mistakes:
  • Confusing unique with sorting
  • Thinking it sums values
  • Assuming it reverses array
2. Which of the following is the correct syntax to get unique values from a NumPy array arr?
easy
A. np.arr.unique()
B. np.unique(arr)
C. unique(arr)
D. arr.unique()

Solution

  1. Step 1: Recall the correct function call

    The function unique is part of the NumPy module and is called as np.unique().
  2. Step 2: Check other options for errors

    arr.unique() is not a NumPy array method, unique(arr) misses the module prefix, and np.arr.unique() is invalid syntax.
  3. Final Answer:

    np.unique(arr) -> Option B
  4. Quick Check:

    Correct syntax = np.unique(arr) [OK]
Hint: Use np.unique(array) always [OK]
Common Mistakes:
  • Calling unique as method on array
  • Missing np prefix
  • Wrong module usage
3. What is the output of this code?
import numpy as np
arr = np.array([3, 1, 2, 3, 2, 1, 4])
print(np.unique(arr))
medium
A. [1 2 3 4]
B. [3 1 2 4]
C. [4 3 2 1]
D. [1 1 2 2 3 3 4]

Solution

  1. Step 1: Identify unique values in the array

    The array contains values 3, 1, 2, 3, 2, 1, 4. Unique values are 1, 2, 3, and 4.
  2. Step 2: Understand np.unique() output order

    np.unique() returns sorted unique values, so output is [1 2 3 4].
  3. Final Answer:

    [1 2 3 4] -> Option A
  4. Quick Check:

    Unique sorted values = [1 2 3 4] [OK]
Hint: Unique values are sorted by default [OK]
Common Mistakes:
  • Ignoring sorting order
  • Listing duplicates
  • Wrong output format
4. The following code throws an error. What is the problem?
import numpy as np
arr = [1, 2, 2, 3]
print(np.unique(arr, return_counts=True))
medium
A. Incorrect argument name, should be count_return=True
B. List input causes error, must convert to np.array first
C. No error, code runs fine
D. np.unique() does not support return_counts argument

Solution

  1. Step 1: Check input type compatibility

    np.unique() accepts lists or arrays as input without error.
  2. Step 2: Verify return_counts argument

    The argument return_counts=True is valid and returns counts of unique values.
  3. Final Answer:

    No error, code runs fine -> Option C
  4. Quick Check:

    List input + return_counts works [OK]
Hint: np.unique accepts lists and arrays [OK]
Common Mistakes:
  • Thinking list input causes error
  • Wrong argument name
  • Assuming return_counts unsupported
5. You have a 2D NumPy array:
arr = np.array([[1, 2, 2], [3, 1, 4]])

How do you get all unique values from this 2D array as a sorted 1D array?
hard
A. np.unique(arr)
B. np.unique(arr, axis=1)
C. np.unique(arr, axis=0)
D. arr.unique()

Solution

  1. Step 1: Understand np.unique() on 2D arrays

    Calling np.unique() without axis flattens the array and returns unique sorted values.
  2. Step 2: Check axis arguments

    Using axis=0 or axis=1 returns unique rows or columns, not unique elements overall.
  3. Final Answer:

    np.unique(arr) -> Option A
  4. Quick Check:

    Flatten and unique = np.unique(arr) [OK]
Hint: No axis means flatten and find unique [OK]
Common Mistakes:
  • Using axis to get unique elements
  • Calling unique as method
  • Expecting 2D unique output