# NumPy Value Counts in Python

This tutorial will introduce the method to determine each unique element’s number of occurrences inside a NumPy array in Python.

## NumPy Value Counts With the `numpy.unique()` Function in Python

If we have an array and want to determine each unique element’s number of occurrences inside the array, we can use the `numpy.unique()` function in Python. The `numpy.unique()` function takes the array as an input argument and returns all the unique elements inside the array in ascending order. We can specify the `return_counts=True` parameter of the `numpy.unique()` function to also get the number of times each element is repeated inside the array. The following code example shows us how to get the number of occurrences of each unique element inside a NumPy array with the `numpy.unique()` function in Python.

``````import numpy as np

array = np.array([1,1,1,2,3,4,4,4])

unique, counts = np.unique(array, return_counts=True)

result = np.column_stack((unique, counts))
print (result)
``````

Output:

``````[[1 3]
[2 1]
[3 1]
[4 3]]
``````

In the above code, we determined the number of occurrences of each unique element inside the NumPy array `array` with the `np.unique()` function in Python. We first created a NumPy array with the `np.array()` function. We then stored all the unique elements of the `array` inside the `unique` array and their respective number of occurrences inside the `counts` array with the `np.unique()` function. We then zipped the two 1-dimensional arrays `unique` and `counts` inside a single 2-dimensional array `result` with the `np.column_stack()` function. In the end, we printed the elements of the `result` array.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

## Related Article - Python Arrays

• Get Column of Numpy Array