0
0
NumpyHow-ToBeginner ยท 3 min read

How to Find Unique Values in NumPy Arrays Easily

Use the np.unique() function to find unique values in a NumPy array. It returns sorted unique elements and can also return counts or indices if needed.
๐Ÿ“

Syntax

The basic syntax of np.unique() is:

  • np.unique(array, return_index=False, return_inverse=False, return_counts=False, axis=None)

Here:

  • array: Input NumPy array to find unique values from.
  • return_index: If True, returns indices of first occurrences of unique values.
  • return_inverse: If True, returns indices to reconstruct original array from unique values.
  • return_counts: If True, returns counts of each unique value.
  • axis: If specified, finds unique rows or columns along this axis.
python
np.unique(array, return_index=False, return_inverse=False, return_counts=False, axis=None)
๐Ÿ’ป

Example

This example shows how to find unique values in a 1D NumPy array and get their counts.

python
import numpy as np

arr = np.array([1, 2, 2, 3, 4, 4, 4, 5])
unique_values, counts = np.unique(arr, return_counts=True)
print("Unique values:", unique_values)
print("Counts:", counts)
Output
Unique values: [1 2 3 4 5] Counts: [1 2 1 3 1]
โš ๏ธ

Common Pitfalls

Some common mistakes when using np.unique() include:

  • Forgetting that the returned unique values are sorted, which may change the original order.
  • Not using return_counts=True when counts are needed.
  • Trying to find unique rows in a 2D array without specifying axis=0.

Example of wrong and right usage:

python
import numpy as np

arr_2d = np.array([[1, 2], [1, 2], [3, 4]])

# Wrong: unique without axis returns unique elements flattened
print(np.unique(arr_2d))

# Right: unique rows by specifying axis=0
print(np.unique(arr_2d, axis=0))
Output
[1 2 3 4] [[1 2] [3 4]]
๐Ÿ“Š

Quick Reference

Summary of np.unique() options:

ParameterDescription
arrayInput array to find unique values from
return_indexReturn indices of first unique values (default False)
return_inverseReturn indices to reconstruct original array (default False)
return_countsReturn counts of each unique value (default False)
axisFind unique rows/columns along this axis (default None)
โœ…

Key Takeaways

Use np.unique() to get sorted unique values from a NumPy array.
Set return_counts=True to get how many times each unique value appears.
Specify axis=0 or axis=1 to find unique rows or columns in 2D arrays.
Remember np.unique() returns sorted results, which may reorder data.
Use return_index or return_inverse to map unique values back to original array.