NumPy 2D array count unique value occurrence

In this, we are going to learn how to count the occurrence of unique values in the NumPy 2D array. The numpy library np.unique() is used to find unique values, their occurrence, and index position.

1. NumPy 2D array count unique value occurrence


The np.unique() method finds unique elements in a NumPy array and returns a numpy array or tuple of array-based on given arguments.

Syntax

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

Parameters

  • array: The array used to find the unique values.
  • return_index: optional and default is False returns a unique array of indices along with the given axis
  • return_inverse: optional and default is False returns a unique array of indices and used to reconstruct of array
  • return_counts: optional and default is False return a unique array of indices
  • axis : optional default is None for columns axis=0 and for rows axis=1.

In this Python program, we are finding all unique values in the whole 2D numpy array without specifying the axis and only by passing the single argument the numpy array.

import numpy as np
nparr = np.array([[1, 2, 9, 4, 2],  
              [6, 9, 8, 9, 4], 
              [11, 9, 13, 9, 15]])


uniqueVal =  np.unique(nparr)

print("unique Value in 2D NumPy array:",  uniqueVal)

Output

unique Value in 2D NumPy array: [ 1  2  4  6  8  9 11 13 15]

Frequently Asked Questions

Numpy 1D array count unique value and frequency


In this Python program, The np. unique() function returns a tuple of two arrays first is unique values and another is frequency count. We have converted both arrays into a single array by using np.asarray() and transposing it.

import numpy as np
nparr = np.array([1, 2, 9, 4, 2, 9, 8, 9, 4,11, 9, 13, 9, 15])

uniqueValues, freCount = np.unique(nparr, return_counts =True)

arrCountFreq = np.asarray((uniqueValues,freCount )).T
print("Numpy array Unique elements:\n",   uniqueValues)

print("occurrence of Value:",  freCount)
print("Value and Frecount as array:\n",  arrCountFreq)

Output

Numpy arra Unique elements:
 [ 1  2  4  8  9 11 13 15]
occurrence of Value: [1 2 2 1 5 1 1 1]
Value and Frecount as array:
 [[ 1  1]
 [ 2  2]
 [ 4  2]
 [ 8  1]
 [ 9  5]
 [11  1]
 [13  1]
 [15  1]]

2. 2D array count unique value occurrence rows and columns


In this Python program, we are finding unique values in rows and columns, their occurrence in a 2D numpy array.

import numpy as np
nparr = np.array([[1, 2, 9, 4, 2],  
              [6, 9, 8, 9, 4], 
              [11, 9, 13, 9, 15]])

unique, ocuurrCount = np.unique(nparr, return_counts = True) 
  
# create a Numpy array
ResArr = np.asarray((unique, ocuurrCount ))
  
print("unique Values and occurrence are:\n",  ResArr)

Output

unique Values and occurrence are:
 [[ 1  2  4  6  8  9 11 13 15]
 [ 1  2  2  1  1  5  1  1  1]]

3. NumPy 2D array count unique value occurrence in columns


To find a unique value in 2D numpy array columns we have to pass the axis=0 and return_counts =True to find the occurrence of unique value in the below Python programs we will understand how to find unique value and its occurrence in 2D numpy array columns.

import numpy as np
nparr = np.array([[1, 2, 9, 4, 2],  
              [6, 9, 8, 9, 4], 
              [11, 9, 13, 9, 15]])

unique, ocuurence = np.unique(nparr, axis =0, return_counts = True)


print("Numpy arra Unique elements:\n", unique)

print("occurrence of Value:",  ocuurence)

Output

Numpy arra Unique elements:
 [[ 1  2  9  4  2]
 [ 6  9  8  9  4]
 [11  9 13  9 15]]
occurrence of Value: [1 1 1]

4.NumPy 2D array count unique value occurrence in row


To find a unique value in 2D numpy array rows we have to pass the axis=1 and return_counts =True to find the occurrence of unique value in the below python programs we will understand how to find unique value and its occurrence in 2D numpy array rows.

import numpy as np
nparr = np.array([[1, 2, 9, 4, 2],  
              [6, 9, 8, 9, 4], 
              [11, 9, 13, 9, 15]])

uniqueRows, ocurrRows = np.unique(nparr, axis =1,  return_counts = True)


print("Numpy arra Unique elements:\n",   uniqueRows)

print("occurrence of Value in rows:",  ocurrRows)

Output

Numpy arra Unique elements:
 [[ 1  2  2  4  9]
 [ 6  4  9  9  8]
 [11 15  9  9 13]]
occurrence of Value in Rows: [1 1 1 1 1]

5. Numpy 2D array count unique value occurrence and Index


In this Python program, We will find the index position of unique value in 2D numpy array np.unique() function with these parameters

  • return_index =True: parameter is used to find index position.
  • return_counts=True find the occurrence of unique value in 2D numpy array.
import numpy as np
nparr = np.array([[1, 2, 9, 4, 2],  
              [6, 9, 8, 9, 4], 
              [11, 9, 13, 9, 15]])


uniqueVal , Index_lst, freCount= np.unique(nparr, return_index=True, return_counts=True)

Val_count_indexlst = zip(uniqueVal, freCount, Index_lst)

for item in Val_count_indexlst:
   print('Value :',item[0], ' occurr :', item[1], 'at index:', item[2])

Output

Value : 1  occurr : 1 at index: 0
Value : 2  occurr : 2 at index: 1
Value : 4  occurr : 2 at index: 3
Value : 6  occurr : 1 at index: 5
Value : 8  occurr : 1 at index: 7
Value : 9  occurr : 5 at index: 2
Value : 11  occurr : 1 at index: 10
Value : 13  occurr : 1 at index: 12
Value : 15  occurr : 1 at index: 14

Summary

In this post, we have learned NumPy 2D array count unique value occurrence with examples