9951 explained code solutions for 126 technologies


python-numpyHow to count unique values


import numpy as np
a = np.array([1,1,2,1,2])
uniq = np.unique(a).sizectrl + c
import numpy as np

load Numpy module for Python

np.array

declare Numpy array

[1,1,2,1,2]

sample array

.unique(

returns unique values only from specified array

.size

attribute contains number of elements in an array

uniq

will contain number of unique elements