# NumPy unique function in Python [7 use cases]

In this Python blog, I will explain how to get unique values in an array using the NumPy unique function in Python. I will explain different use cases for the NumPy unique function like np.unique without sorting, NumPy unique with tolerance, etc.

To get unique values in an array, we can use the NumPy unique function in Python. This identifies elements in an array, with options for additional functionality. Its basic use returns sorted unique values, but parameters like return_index, return_inverse, and return_counts provide indices, inverse indices, and counts of unique elements, respectively. For unsorted results, combine unique with data[np.sort(idx)]. To handle tolerance, preprocess the array with np.round to a desired precision before applying unique, effectively grouping close values as identical.

## NumPy unique function in Python

The NumPy unique function in Python is used to find the unique elements of an array. It returns the sorted unique elements of an array, providing options to return indices, the number of times each unique item appears, and a reversed index.

Example: Let’s take an array and try to get unique values using the NumPy unique function in Python.

``````import numpy as np
arr = np.array([1, 2, 2, 3, 3, 3, 4])
unique_elements = np.unique(arr)
print('The unique values of the input array is:\n', unique_elements)``````

Output: Here, simply the np.unique() function in Python will return all the unique values from the input array.

``````The unique values of the input array is:
[1 2 3 4]``````

### NumPy unique Syntax and Parameters

The function is called as follows:

``numpy.unique(arr, return_index=False, return_inverse=False, return_counts=False, axis=None)``

Here,

## np.unique function in Python use case

There can be different use cases if we provide different parameters in the NumPy unique function in Python. Let’s see them one by one:

### Case 1: Python NumPy unique function basic function

It finds and returns unique elements from the array in Python.

Example: Imagine we have an array in Python and want to filter only the unique values from it through the np.unique() function.

``````import numpy as np

states = np.array(['California', 'Texas', 'New York', 'California', 'Texas'])
unique_states = np.unique(states)
print('The unique states in the input array are:\n', unique_states)``````

Output: This will return unique values from the array in Python.

``````The unique states in the input array are:
['California' 'New York' 'Texas']``````

This way we can simply use the NumPy unique function in Python.

### Case 2: unique NumPy function with return_index parameter

The numpy.unique with return_index parameter provides unique elements and their first occurrence indices in the NumPy array.

``````import numpy as np

cities = np.array(["New York", "Los Angeles", "Chicago", "New York", "Los Angeles"])
unique_cities = np.unique(cities, return_index=True)
print('The unique cities in the input array are:\n', unique_cities)``````

Output: The implementation of the code is mentioned below:

``````The unique cities in the input array are:
(array(['Chicago', 'Los Angeles', 'New York'], dtype='<U11'), array([2, 1, 0], dtype=int64))``````

This way we can simply use the NumPy unique function in Python with the return_index parameter.

### Case 3: numpy.unique function with return_inverse parameter

The np.unique() with return_inverse parameter returns unique elements and indices to reconstruct `arr` from these unique elements.

``````import numpy as np

presidents = np.array(["Washington", "Jefferson", "Lincoln", "Roosevelt", "Roosevelt"])
unique_presidents_names, inverse_indices = np.unique(presidents, return_inverse=True)
print('The unique presidents names in the input array are:\n', unique_presidents_names)
print('The indices where the names are repeated: ', inverse_indices)``````

Output: After implementation of the code we get:

``````The unique presidents names in the input array are:
['Jefferson' 'Lincoln' 'Roosevelt' 'Washington']
The indices where the names are repeated:  [3 0 1 2 2]``````

This way we can simply use the NumPy unique function in Python with the return_inverse parameter.

### Case 4: np.unique() function with return_counts parameter

The np.unique() function with return_counts parameter delivers unique elements and the count of their occurrences in the Python array.

``````import numpy as np

car_brands = np.array(['Ford', 'Toyota', 'Ford', 'Honda', 'Toyota', 'Honda'])
unique_brands, counts = np.unique(car_brands, return_counts=True)
print('The unique brands names in the input array are:\n', unique_brands)
print('The number of times names are repeated are: ', counts)``````

Output: The implementation of the code is mentioned below:

``````The unique brands names in the input array are:
['Ford' 'Honda' 'Toyota']
The number of times names are repeated are:  [2 2 2]``````

This way we can simply use the NumPy unique function in Python with the return_counts parameter.

### Case 5: unique values from rows and columns in NumPy Python

The np.unique() with axis parameter identifies unique rows (or columns if axis=1) in a 2D array Python.

Example 1: Here, we will use the axis parameter assigned with 0 to get only unique rows.

``````import numpy as np

demographic_data = np.array([[30, 55000], [40, 65000], [30, 55000], [40, 70000]])
unique_demographics = np.unique(demographic_data, axis=0)
print('The unique demographics data in the input array are:\n', unique_demographics)``````

Output: The implementation of the code:

``````The unique demographics data in the input array are:
[[   30 55000]
[   40 65000]
[   40 70000]]``````

Example 2: Here, we will use the axis parameter assigned with 1 to get only unique columns.

``````import numpy as np

sales_data = np.array([
[1200, 1500, 1200],  # Month 1
[1300, 1500, 1300],  # Month 2
[1250, 1600, 1250]   # Month 3
])

unique_sales_columns = np.unique(sales_data, axis=1)
print(unique_sales_columns)``````

Output: The output of the code is mentioned below:

``````[[1200 1500]
[1300 1500]
[1250 1600]]``````

### Case 6: np.unique without sorting

As of the last update in April 2023, the numpy.unique function in Python does not provide a built-in option to return the unique elements without sorting them. However, we can achieve a non-sorted unique list by using a combination of other NumPy functions and Python constructs.

``````import numpy as np

data = np.array([20, 10, 50, 10, 30, 20, 40])
_, idx = np.unique(data, return_index=True)
unique_without_sort = data[np.sort(idx)]
print(unique_without_sort)``````

Output: Here, data[np.sort(idx)] extracts the elements from the original array in the order of their first appearance, resulting in unique elements without sorting them based on their value through Python.

``[20 10 50 30 40]``

This way we can get the unique values from the array with sorting in Python.

### Case 7: NumPy unique with tolerance

The NumPy unique function in Python with tolerance is achieved by rounding array elements to a specified number of decimal places before applying np.unique, treating values as the same if they round to the same number.

``````import numpy as np

measurements = np.array([1.01, 1.02, 1.003, 1.0004, 2.1, 2.05, 2.095])
tolerance = 2
rounded_measurements = np.round(measurements, decimals=tolerance)
unique_measurements = np.unique(rounded_measurements)

print(unique_measurements)``````

Output: The implementation of the Python code:

``[1.   1.01 1.02 2.05 2.1 ]``

## Conclusion

The NumPy unique function in Python is a versatile and powerful tool for data analysis, capable of handling both simple and complex scenarios. This function offers an array of options to cater to various data processing needs, making it an indispensable part of the Python data scientist’s toolkit with different parameters such as return_index, return_inverse, return_count, and axis.

You may also like to read: