numpy.unique_counts#

numpy.unique_counts(x)[source]#

Find the unique elements and counts of an input array x.

This function is an Array API compatible alternative to:

np.unique(x, return_counts=True, equal_nan=False)

but returns a namedtuple for easier access to each output.

Parameters:
xarray_like

Input array. It will be flattened if it is not already 1-D.

Returns:
outnamedtuple

The result containing:

  • values - The unique elements of an input array.

  • counts - The corresponding counts for each unique element.

See also

unique

Find the unique elements of an array.

Examples

>>> import numpy as np
>>> x = [1, 1, 2]
>>> uniq = np.unique_counts(x)
>>> uniq.values
array([1, 2])
>>> uniq.counts
array([2, 1])