Python Numpy unique – Complete Tutorial

In this Python tutorial, we will learn how to get unique values from the Numpy array by using Python. Also, we will cover these topics.

  • Python numpy unique 2d array
  • Python numpy unique values
  • Python numpy unique without sort
  • Python numpy unique rows
  • Python numpy unique count
  • Python numpy unique with tolerance
  • Python numpy unique return_index
  • Python numpy unique axis
  • Python unique without numpy
  • Python np unique return_inverse
  • Python np.unique string

Python numpy unique

  • In this section we will discuss how to find unique elements in a numpy array by using Python.
  • In this example, we are going to use the np.unique() function. In Python, this method is used to identify the unique elements in a NumPy array and this function returns the indices of the input array which gives the unique elements.
  • In Python, the numpy module provides a numpy.unique() elements and this function also identifies the unique rows and columns.

Syntax:

Here is the Syntax of np.unique() function

numpy.unique
            (
             ar,
             return_index=False,
             return_inverse=False,
             return_counts=False,
             axis=None
            )
  • It consists of the few parameters
    • ar: This parameter indicates which array you want to operate on and it will be flattened.
    • return_index: This parameter always returns the index of the elements and by default, it takes as ‘False’ value.
    • return_inverse: It is an optional parameter and always returns the number of times each unique value in the input array.

Example:

Let’s take an example and check how to use numpy.unique() function in Python

Source code:

import numpy as np  

new_val=np.array([98,34,56,89,45,56,34,20])  
result=np.unique(new_val)
print("Unique elements:",result)

In the above code, we have imported a numpy library and then declare a variable ‘new_val’ and assigned the function np.unique() and within this function, we have set the ‘new_val’ variable. Once you will print ‘result’ then the output will display the unique elements.

Here is the implementation of the following given code

Python numpy unique
Python numpy unique

Also, check: Python NumPy 3d array

Python numpy unique 2d array

  • In this program, we will discuss how to identify unique values from a 2-dimensional array in Python.
  • In this example, we are going to use the np.unique() function and assign the axis=0 that signifies a direction along which to use the np.unique() function.
  • In this program, we have created duplicated values in a 2-dimensional array. Now by using the np.unique() function we will get the unique elements from the input array.

Example:

import numpy as np  

new_arr = np.array([[67, 98, 67], [67, 67, 5], [45, 5, 67]])  
result=np.unique(new_arr, axis=0)  
print(result)

Here is the execution of the following given code

Python numpy unique 2d array
Python numpy unique 2d array

Read: Python NumPy 2d array

Python numpy unique values

  • Let us see how to find the unique values from a numpy array in Python.
  • By using the np.unique() method we can easily identify the unique values from the numpy array.
  • To do this task first we are going to create a simple array by using the np.array() function and then declare a variable ‘new_output’ and assign a unique() function in it. Once you will print ‘new_output’ then the result will display the unique values.

Example:

import numpy as np  

new_val=np.array([85,34,85,99,23,109,34,109])  
new_output=np.unique(new_val)
print("Unique values:",new_output)

Here is the Screenshot of the following given code

Python numpy unique values
Python numpy unique values

As you can see in the Screenshot the output is generated unique elements.

Read: Python NumPy Split

Python numpy unique without sort

  • In this section, we will discuss how to get the unique values without sorting in NumPy Python.
  • Here we can use the np.unique() method in which we have to assign ‘return_index’ as an argument and if it is true then this function will return the index of the first occurrence of the unique element.

Source Code:

import numpy as np

new_arr = [16,11,5,9,9,11,5,16]
new_values = np.unique(new_arr, return_index=True)[1]
b=[new_arr[index] for index in sorted(new_values)]
print(b)

Here is the implementation of the following given code

Python numpy unique without sort
Python numpy unique without sort

As you can see in the Screenshot the output will display the unique values without sort.

Read: Python NumPy Normalize

Python numpy unique rows

  • In this section, we will discuss how to get the unique rows from the NumPy array in Python.
  • To get the unique rows from an array, we set axis=0 and the np.unique function will help the user to operate downwards in the axis-0 direction, and if the axis=1 then it operates horizontally and finds the unique column values.
  • In this example, we are going to use a numpy library and then apply the np.array() function for creating an array. Now declare a variable ‘result’ an assigned np.unique() function in it along with axis.

Source Code:

import numpy as np

new_array = np.array([[43, 43, 34, 43],
					[29, 43, 34, 43],
					[89, 43, 34, 43],
					[43, 43, 34, 43]])

result = np.unique(new_array,axis = 0)
print(result)

Here is the implementation of the following given code

Python Numpy unique rows
Python Numpy unique rows

As you can see in the Screenshot the output displays the unique rows.

Read: Python NumPy Random

Python numpy unique count

  • In this Program, we will discuss how to count unique values from a numpy array in Python.
  • To do this task we are going to use the return_counts=True as a parameter and this will return the number of times each unique elements occur in the numpy array.
  • In Python, the numpy.column_stack is used for converting 1-d array as columns into a 2-d array and it is just like a hstack() function.

Syntax:

Here is the Syntax of numpy.column_stack() method

numpy.column_stack
                  (
                   tup
                  )

Example:

import numpy as np

new_val = np.array([75,75,54,75,54,54,67,54])

new_count = np.unique(new_val, return_counts=True)
final_output = np.column_stack((new_count)) 
print(final_output)

In the above code, we have used the np.unique() function and assigned the array ‘new_val’ as an argument. Now use the np.column_stack() method for counting the unique value.

You can refer to the below Screenshot

Python numpy unique count
Python numpy unique count

Read: Python NumPy max

Python numpy unique with tolerance

  • In this program, we will discuss how to find unique elements with tolerance in the NumPy array.
  • To do this first we will set the value 2.0e-4 and then use the np.random() function for creating an array. Now apply the argsort() method and assign arr ‘new_arr’ in it. In Python, the argsort() method is used to compute the indirect sorting of a NumPy array.
  • Now use append() function and pass ‘True’ and ‘np.diff’ as an argument. In Python the np.diff is used to measure the nth discrete difference along with the given axis.

Source Code:

import numpy as np

new_val = 2.0e-4
new_arr = np.random.random((8,8))
out = np.argsort(new_arr.flat)
x = np.append(True, np.diff(new_arr.flat[out]))
final_output = new_arr.flat[out[x>new_val]]
print(final_output)

Here is the Screenshot of the following given code

Python numpy unique with tolerance
Python numpy unique with tolerance

As you can see in the Screenshot the output displays the unique value with a value.

Read: Python reverse NumPy array

Python numpy unique return_index

  • In this section, we will discuss how to get unique values by using return_index in Python.
  • To perform this particular task we are going to use the np.unique() function and assign retun_index=True as an argument. In Python, if this argument is set=True then the np.unique() function will always return the index of the NumPy array along with the specified axis.

Example:

import numpy as np

new_val = np.array([65,65,71,86,95,32,65,71,86,55,76,71])
b= np.unique(new_val, return_index=True)
print(b)

In the above code first, we have imported a numpy library and then use the np.array() function for creating an array. Now declare a variable ‘b’ and pass ‘new_val’ as an argument. Once you will print ‘b’ then the output will display unique values along with indexes.

You can refer to the below Screenshot

Python numpy unique return_index
Python numpy unique return_index

Read: Python NumPy shape with examples

Python numpy unique axis

  • Let us see how to identify unique values along with axis in NumPy array by using Python.
  • By using np.unique() function, we can solve this problem. In this program, we are going to set the axis parameter in the np.unique() function. This parameter signifies a direction along which to use numpy.unique() function by default it takes none value.
  • In this program, we have set the parameter axis=0 and 1 that indicates the unique value will be displayed vertically and horizontally.

Source Code:

import numpy as np

new_arr = np.array([[76, 29, 29], [76, 29, 29], [56, 96, 156]])
b= np.unique(new_arr, axis=0)
d= np.unique(new_arr, axis=1)
print(b)
print(d)

Here is the implementation of the following given code

Python numpy unique axis
Python numpy unique axis

Read: Python NumPy empty array

Python unique without numpy

  • In this section, we will discuss how to get the unique values in Python without numpy.
  • In this program first, we will create a list and then use the counter function from the collection library along with we will create a list by using the counter() function. Now by using keys from the dictionary we will get the unique values.

Example:

from collections import Counter
 
new_list = [21, 63, 47, 21, 63, 99, 63, 4, 63, 99]
result = Counter(new_list).keys() 
print(result)

In the above code we have taken the list ‘new_list’ and then applied the counter () function in which the elements are stored as dictionary keys and their counts are considered as dictionary values.

Here is the Screenshot of the following given code

Python unique without numpy
Python unique without numpy

As you can see in the Screenshot the output displays the unique value from the list.

Read: Python NumPy Average

Python np unique return_inverse

  • In this Program, we will discuss how to get the unique values in NumPy Python by using the return_inverse parameter.
  • In this example first, we are going to use the np.unique() function in which we will be assigned return_inverse=True as an argument. If return_inverse=True then this function will return the indices of the unique array and by default, this parameter takes the ‘False’ value.

Syntax:

Here is the Syntax of np.unique() function

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

Example:

import numpy as np

new_array = np.array([65,65,71,86,95,32,65,71,86,55,76,71])
result= np.unique(new_array, return_inverse=True)
print(result)

Here is the execution of the following given code

Python np unique return inverse
Python np unique return inverse

As you can see in the Screenshot the output display the inverse index number with unique values.

Read: Python NumPy square with examples

Python np.unique string

  • In this section, we will discuss how to find unique values in NumPy Python by using string.
  • To do this task first we will declare a list and assign string and integers value to it. Now use the np.unique() function and pass arr, return_counts as an argument.

Source Code:

import numpy as np

my_new_arr = [('L',56,34,66),('M',21,30,97),('N',56,34,66)]
final_result = np.unique(my_new_arr,return_counts=True, axis=0)
print(final_result)

You can refer to the below Screenshot

Python np unique string
Python np unique string

As you can see in the Screenshot, the output displays the array that contains a unique value along with string character.

You may also like to read the following tutorials on Python Numpy.

So, in this Python tutorial, we have learned how to get unique values from the Numpy array by using Python. Also, we have covered these topics.

  • Python numpy unique 2d array
  • Python numpy unique values
  • Python numpy unique without sort
  • Python numpy unique rows
  • Python numpy unique count
  • Python numpy unique with tolerance
  • Python numpy unique return_index
  • Python numpy unique axis
  • Python unique without numpy
  • Python np unique return_inverse
  • Python np.unique string