Count Unique Values in NumPy Array

Count unique values in numpy array

1. Introduction

One of the common tasks Numpy Users may encounter is count unique values in Numpy Array that can help in exploring the distribution of Nummy Array. In this article, we will see different ways to count unique values in Numpy Array.

2. Using np.unique() Method with len() Method

Use np.unique() method with len() method to count unique values in Numpy array.

Running the above code will display the following output on the console:

np.unique() method finds unique values in the array and len() function counts number of elements in the array.

3. Using np.unique() method to display unique values

Use np.unique() to display unique values in numpy array.

Running the above code will display the following output on the console:

np.unique() method finds unique values in the array and returns sorted unique values.

4. Use np.unique() Method with return_counts as true

Use np.unique() method with return_counts flag as true to count occurrences of each unique element in Numpy array.

Let’s assume we already have installed a Python library named numpy to work with arrays. In case, we can use pip install numpy to install this if we don’t have it.

To count occurrences of each element in the NumPy array:

  • Use np.array() to create a NumPy array.
  • Use np.unique() to find unique values and their count.
  • Use lambda function to execute an expression (u, c), unique_values, count
  • Use map() function to apply lambda() function to every element of unique_values and count.
  • Use dict() to create a dictionary containing key-value pair where key is unique_value and value is the count.

Running the above code will display the following output on the console:

We used np.array() to generate a numpy array, which stored unique and redundant values to identify unique elements.

The function np.unique() used the array and return_counts=True as parameters. It created a new array of unique_elements and stored the count of these elements in the count variable.

The variable result is a Python dictionary created with the method dict() that receives the mapping of a lambda() function.

  • The lambda() function is anonymous and has no name.
  • The map() function is a one-line iterator that applied the lambda() function to each element of unique_values and count.
  • The dict() function created a dictionary result with unique_value as the key and count as the value.

5. Count Specific Values in Numpy Array

To count specific values in Numpy Array, We can use np.count_nonzero() method with condition.

Here is an example to count number of elements that are equal to 3:

Running the above code will display the following output on the console:

6. Count Unique Rows in 2D Array

To count unique rows in 2D array, use axis=0 option with 2D array.

Output:

7. Conclusion

In this article, we explored different options for count unique value in Numpy Array. We can apply these options in Practice based on our need.

np.unique() with len() function is useful when we want to count unique value. In case we want to count occurrences of each unique value, np.unique with return_counts option is useful.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *