Methods to find unique values in a numpy array with frequency and indices
In this article, we will discuss how to find unique values, rows, and columns in a 1D & 2D Numpy array. Before going to the methods first we see numpy.unique() method because this method is going to be used.
numpy.unique() method
numpy.unique() method help us to get the unique() values from given array.
syntax:numpy.unique(array, return_index=False, return_inverse=False, return_counts=False, axis=None)
Parameters
- array-Here we have to pass our array from which we want to get unique value.
- return_index- If this parameter is true then it will return the array of the index of the first occurrence of each unique value. By default it is false.
- return_counts-If this parameter is true then it will return the array of the count of the occurrence of each unique value. By default it is false.
- axis- It is used in the case of nd array, not in 1d array. axis=1 means we have to do operation column-wise and axis=0 means we have to do operation row-wise.
Now we will see different methods to find unique value with their indices and frequencies in a numpy array.
case 1-When our array is 1-D
Method 1-Find unique value from the array
As we only need unique values and not their frequencies and indices hence we simply pass our numpy array in the unique() method because the default value of other parameters is false so we don’t need to change them. Let see this with the help of an example.
import numpy as np arr = np.array([1, 1, 2, 3, 4, 5, 6, 7, 2, 3, 1, 4, 7]) unique_values=np.unique(arr) print("Original array is") print(arr) print("------------------") print("Unique values are") print(unique_values)
Output
Original array is [1 1 2 3 4 5 6 7 2 3 1 4 7] ------------------ Unique values are [1 2 3 4 5 6 7]
Method 2-Find unique value from the array along with their indices
In this method, as we want to get unique values along with their indices hence we make the return_index parameter true and pass our array. Let see this with the help of an example.
import numpy as np arr = np.array([1, 1, 2, 3, 4, 5, 6, 7, 2, 3, 1, 4, 7]) unique_values,index=np.unique(arr,return_index=True) print("Original array is") print(arr) print("------------------") print("Unique values are") print(unique_values) print("First index of unique values are:") print(index)
Output
Original array is [1 1 2 3 4 5 6 7 2 3 1 4 7] ------------------ Unique values are [1 2 3 4 5 6 7] First index of unique values are: [0 2 3 4 5 6 7]
Method 3-Find unique value from the array along with their frequencies
In this method, as we want to get unique values along with their frequencies hence we make the return_counts parameter true and pass our array. Let see this with the help of an example.
import numpy as np arr = np.array([1, 1, 2, 3, 4, 5, 6, 7, 2, 3, 1, 4, 7]) unique_values,count=np.unique(arr,return_counts=True) print("Original array is") print(arr) print("------------------") print("Unique values are") print(unique_values) print("Count of unique values are:") for i in range(0,len(unique_values)): print("count of ",unique_values[i]," is ",count[i])
Output
Original array is [1 1 2 3 4 5 6 7 2 3 1 4 7] ------------------ Unique values are [1 2 3 4 5 6 7] Count of unique values are: count of 1 is 3 count of 2 is 2 count of 3 is 2 count of 4 is 2 count of 5 is 1 count of 6 is 1 count of 7 is 2
Case 2: When our array is 2-D
Method 1-Find unique value from the array
Here we simply pass our array and all the parameter remain the same. Here we don’t make any changes because we want to work on both rows and columns. Let see this with the help of an example.
import numpy as np arr = np.array([[1, 1, 2,1] ,[ 3, 1, 2,1] , [ 6, 1, 2, 1], [1, 1, 2, 1]]) unique_values=np.unique(arr) print("Original array is") print(arr) print("------------------") print("Unique values are") print(unique_values)
Output
Original array is
[[1 1 2 1]
[3 1 2 1]
[6 1 2 1]
[1 1 2 1]]
------------------
Unique values are
[1 2 3 6]
Method 2-Get unique rows
As here want to want to work only on rows so here we will make axis=0 and simply pass our array. Let see this with the help of an example.
import numpy as np arr = np.array([[1, 1, 2,1] ,[ 3, 1, 2,1] , [ 6, 1, 2, 1], [1, 1, 2, 1]]) unique_values=np.unique(arr,axis=0) print("Original array is") print(arr) print("------------------") print("Unique rows are") print(unique_values)
Output
Original array is [[1 1 2 1] [3 1 2 1] [6 1 2 1] [1 1 2 1]] ------------------ Unique rows are [[1 1 2 1] [3 1 2 1] [6 1 2 1]]
Method 3-Get unique columns
As here want to want to work only on columns so here we will make axis=1 and simply pass our array. Let see this with the help of an example.
import numpy as np arr = np.array([[1, 1, 2,1] ,[ 3, 1, 2,1] , [ 6, 1, 2, 1], [1, 1, 2, 1]]) unique_values=np.unique(arr,axis=1) print("Original array is") print(arr) print("------------------") print("Unique columns are") print(unique_values)
Output
Original array is [[1 1 2 1] [3 1 2 1] [6 1 2 1] [1 1 2 1]] ------------------ Unique columns are [[1 1 2] [1 3 2] [1 6 2] [1 1 2]]
so these are the methods to find unique values in a numpy array with frequency and indices.