Find and count Numpy array frequency of unique row and column values

This short recipe will guide us through identifying and counting unique values in a Numpy array (Ndarray)

Creating our NumPy ndarrays

We’ll first a couple of Numpy arrays for use to use in our examples.

first_array = np.array ([1000, 2000, 5000, 7000, 8000, 1000, 2000])
second_array = np.array([[2000, 2000, 5000, 7000, 8000, 1000, 2000],
                         [2000, 2000, 5000, 2000, 8000, 1000, 2000]])

Using numpy.unique() to count Numpy unique array values

Run the following code:

np.unique(first_array)

We’ll get a new array showing the distinct values

array([1000, 2000, 5000, 7000, 8000])

We can sum the overall distinct values using the len function:

len(np.unique(first_array))

Will return 5.

Counting frequencies of distinct values

We’ll add the return_counts = True parameter to get the frequencies:

np.unique(first_array, return_counts = True)

Will return the following tuple of ndarrays:

(array([1000, 2000, 5000, 7000, 8000]), array([2, 2, 1, 1, 1], dtype=int64))

Frequency of distinct rows in a array

The following code will return the number of unique rows in our array list:

np.unique(second_array, axis=0)

Note: setting axis=1 will return the distinct columns in our multi dimensional array.

We can also find out easily the number of specific values in a specific row. Here’s an example for the first array row:

len(np.unique(second_array[1]))

Leave a Comment