Unique¶
In [1]:
%pip install numpy
import numpy as np
Requirement already satisfied: numpy in /home/samrat/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages (1.24.4) Note: you may need to restart the kernel to use updated packages.
In [3]:
gar = np.array(['b', 'b', 'a', 'a', 'c', 'c'])
np.unique(gar)
Out[3]:
array(['a', 'b', 'c'], dtype='<U1')
In [4]:
np.unique(gar, return_index=True)
Out[4]:
(array(['a', 'b', 'c'], dtype='<U1'), array([2, 0, 4]))
In [5]:
unique, first_positions = np.unique(gar, return_index=True)
unique[np.argsort(first_positions)]
Out[5]:
array(['b', 'a', 'c'], dtype='<U1')
In [6]:
np.unique(gar, return_counts=True)
Out[6]:
(array(['a', 'b', 'c'], dtype='<U1'), array([2, 2, 2]))