Numpy np.unique() method-Python
numpy.unique() finds the unique elements of an array. It is often used in data analysis to eliminate duplicate values and return only the distinct values in sorted order. Example:
import numpy as np
a = np.array([1, 2, 2, 3, 4, 4, 4])
res = np.unique(a)
print(res)
Output
[1 2 3 4]
Explanation: numpy.unique() removes duplicates and returns only the unique sorted values from the array.
Syntax
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
Parameter:
Parameter | Description |
---|---|
ar | Input array flattened if not 1-D unless axis is specified. |
return_index | If True, returns indices of first occurrences of unique values. |
return_inverse | If True, returns indices to reconstruct the original array. |
return_counts | If True, returns the count of each unique value. |
axis | Operates along the given axis and if None, the array is flattened. |
Returns:
- A sorted 1-D array of unique values.
- Optional arrays depending on return_index, return_inverse, and return_counts.
Examples
Example 1: In this example, we use the return_counts=True parameter to get both the unique elements and how many times each value appears in the array.
import numpy as np
a = np.array([1, 2, 2, 3, 3, 3])
res, counts = np.unique(a, return_counts=True)
print("Unique:", res)
print("Counts:", counts)
Output
Unique: [1 2 3] Counts: [1 2 3]
Explanation: np.unique(a, return_counts=True) returns both unique values and how many times each occurs.
Example 2: In this example, we use the return_inverse=True parameter to get an array that can be used to reconstruct the original array using the unique values.
import numpy as np
a = np.array([3, 1, 2, 1])
unique, inverse = np.unique(a, return_inverse=True)
print("Unique:", unique)
print("Inverse:", inverse)
Output
Unique: [1 2 3] Inverse: [2 0 1 0]
Explanation: Inverse array contains indices such that unique[inverse] reconstructs the original array.
Example 3: In this example, we use the return_index=True parameter to find the indices of the first occurrences of the unique values in the original array.
import numpy as np
a = np.array([4, 3, 3, 2, 1, 2])
unique, indices = np.unique(a, return_index=True)
print("Unique:", unique)
print("Indices:", indices)
Output
Unique: [1 2 3 4] Indices: [4 3 1 0]
Explanation: Indices array tells the index in the original array where each unique element first appeared.