We use np.unique() to find all the different values in a list or array. It helps us see what unique items are there without repeats.
np.unique() for unique values in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
ar is the input array or list.
You can ask for extra info like where the unique values came from or how many times they appear by setting return_index, return_inverse, or return_counts to True.
Examples
NumPy
import numpy as np arr = [1, 2, 2, 3, 4, 4, 4] unique_values = np.unique(arr) print(unique_values)
NumPy
arr = np.array([5, 3, 5, 2, 3]) unique_vals, counts = np.unique(arr, return_counts=True) print(unique_vals) print(counts)
NumPy
arr = np.array([[1, 2], [2, 3]]) unique_axis0 = np.unique(arr, axis=0) print(unique_axis0)
Sample Program
This program shows how to get unique values from an array and also count how many times each unique value appears.
NumPy
import numpy as np # Sample data with repeated values data = np.array([10, 20, 20, 30, 10, 40, 50, 50, 50]) # Find unique values unique_vals = np.unique(data) print("Unique values:", unique_vals) # Find unique values and their counts unique_vals, counts = np.unique(data, return_counts=True) print("Counts of each unique value:", counts)
Important Notes
np.unique() always returns sorted unique values.
Use return_counts=True to see how often each unique value appears.
For multi-dimensional arrays, use the axis parameter to find unique rows or columns.
Summary
np.unique() helps find all different values in data.
You can get extra info like counts or original positions.
It works on lists and arrays, including multi-dimensional ones.
Practice
1. What does the
np.unique() function do in NumPy?easy
Solution
Step 1: Understand the purpose of
This function is designed to find all different (unique) values in a NumPy array or list.np.unique()Step 2: Compare with other options
Sorting, summing, or reversing are different operations and not whatnp.unique()does.Final Answer:
Finds all unique values in an array -> Option DQuick 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
Solution
Step 1: Recall the correct function call
The functionuniqueis part of the NumPy module and is called asnp.unique().Step 2: Check other options for errors
arr.unique()is not a NumPy array method,unique(arr)misses the module prefix, andnp.arr.unique()is invalid syntax.Final Answer:
np.unique(arr) -> Option BQuick 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
Solution
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.Step 2: Understand
np.unique()output ordernp.unique()returns sorted unique values, so output is [1 2 3 4].Final Answer:
[1 2 3 4] -> Option AQuick 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
Solution
Step 1: Check input type compatibility
np.unique()accepts lists or arrays as input without error.Step 2: Verify
The argumentreturn_countsargumentreturn_counts=Trueis valid and returns counts of unique values.Final Answer:
No error, code runs fine -> Option CQuick 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:
How do you get all unique values from this 2D array as a sorted 1D 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
Solution
Step 1: Understand
Callingnp.unique()on 2D arraysnp.unique()without axis flattens the array and returns unique sorted values.Step 2: Check axis arguments
Usingaxis=0oraxis=1returns unique rows or columns, not unique elements overall.Final Answer:
np.unique(arr) -> Option AQuick 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
