IKH

chapter 22

How to find unique items and count

using unique() function we can find the unique items and their count in ndarray.

unique(ar, return_index=False, return_inverse=False, return_counts=False,
axis=None) ==> Find the unique elements of an array.

Returns the sorted unique elements of an array.

There are three optional outputs in addition to the unique elements:

  • The indices of the input array that give the unique values ==> return_index.
  • The indices of the unique array that reconstruct the input array.
  • the number of times each unique value comes up in the input array ==>return_counts.

Example

Python
import numpy as np
help(np.unique)

Output

PowerShell
Help on function unique in module numpy:

unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
    Find the unique elements of an array.

Get array with unique elements

Example

Python
a = np.array([1,1,2,3,4,2,3,4,4,1,2,3,4,5,5,6])
print(f"Original array : {a}")
print(f"Unique elements in the array using np.unique(a) : {np.unique(a)}")

Output

PowerShell
Original array : [1 1 2 3 4 2 3 4 4 1 2 3 4 5 5 6]
Unique elements in the array using np.unique(a) : [1 2 3 4 5 6]

Get indices also

Example

Python
a = np.array([1,1,2,3,4,2,3,4,4,1,2,3,4,5,5,6])
items,indices = np.unique(a,return_index=True)
print(f"Original array : {a}")
print(f"Unique Elements :{items}")
print(f"indices of unique elements: {indices}")

Output

PowerShell
Original array : [1 1 2 3 4 2 3 4 4 1 2 3 4 5 5 6]
Unique Elements :[1 2 3 4 5 6]
indices of unique elements: [ 0 2 3 4 13 15]

To get count also

Example

Python
a = np.array([1,1,2,3,4,2,3,4,4,1,2,3,4,5,5,6])
items,counts = np.unique(a,return_counts=True)
print(f"Original array : {a}")
print(f"Unique Elements :{items}")
print(f"count of unique elements: {counts}")

Output

PowerShell
Original array : [1 1 2 3 4 2 3 4 4 1 2 3 4 5 5 6]
Unique Elements :[1 2 3 4 5 6]
count of unique elements: [3 3 3 4 2 1]

To get all

Python
a = np.array([1,1,2,3,4,2,3,4,4,1,2,3,4,5,5,6])
items,indices,counts = np.unique(a,return_index=True,return_counts=True)
print(f"Original array : {a}")
print(f"Unique Elements :{items}")
print(f"indices of unique elements: {indices}")
print(f"count of unique elements: {counts}")

Output

PowerShell
Original array : [1 1 2 3 4 2 3 4 4 1 2 3 4 5 5 6]
Unique Elements :[1 2 3 4 5 6]
indices of unique elements: [ 0 2 3 4 13 15]
count of unique elements: [3 3 3 4 2 1]

Example

Python
# To get all in a program

import numpy as np
a = np.array(['a','a','b','c','a','a','b','c','a','b','d'])
items,indices,counts = np.unique(a,return_index=True,return_counts=True)
for item,index,count in zip(np.nditer(items),np.nditer(indices),np.nditer(counts)):
  print(f"Element '{item}' occurred {count} times and its first occurrence
index:{index}")

Output

PowerShell
Element 'a' occurred 5 times and its first occurrence index:0
Element 'b' occurred 3 times and its first occurrence index:2
Element 'c' occurred 2 times and its first occurrence index:3
Element 'd' occurred 1 times and its first occurrence index:10