Table of Contents
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.
|
1 2 3 4 5 6 7 |
import numpy as np ## generate a numpy array array = np.array([1, 2, 3, 3, 4, 5, 5, 6]) result = len(np.unique(array)) print(result) |
Running the above code will display the following output on the console:
|
1 2 3 |
6 |
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.
|
1 2 3 4 5 6 7 |
import numpy as np ## generate a numpy array array = np.array([1, 2, 3, 3, 4, 5, 5, 6]) result = np.unique(array) print(result) |
Running the above code will display the following output on the console:
|
1 2 3 |
[1 2 3 4 5 6] |
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 aNumPyarray. - Use
np.unique()to find unique values and their count. - Use
lambdafunction to execute an expression(u, c), unique_values, count - Use
map()function to applylambda()function to every element ofunique_valuesandcount. - Use
dict()to create a dictionary containing key-value pair wherekeyisunique_valueandvalueis thecount.
|
1 2 3 4 5 6 7 8 |
import numpy as np ## generate a numpy array array = np.array([1, 2, 3, 3, 4, 5, 5, 6]) unique_values, count = np.unique(array, return_counts=True) result = dict(map(lambda u, c: (u, c), unique_values, count)) print(result) |
Running the above code will display the following output on the console:
|
1 2 3 |
{1: 1, 2: 1, 3: 2, 4: 1, 5: 2, 6: 1} |
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 thelambda()function to each element ofunique_valuesandcount. - The
dict()function created a dictionaryresultwithunique_valueas thekeyandcountas thevalue.
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:
|
1 2 3 4 5 |
array = np.array([1, 2, 3, 3, 4, 5, 5, 6]) count_3 =np.count_nonzero(array==3) print(count_3) |
Running the above code will display the following output on the console:
|
1 2 3 |
2 |
6. Count Unique Rows in 2D Array
To count unique rows in 2D array, use axis=0 option with 2D array.
|
1 2 3 4 |
a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]]) np.unique(a, axis=0) |
Output:
|
1 2 3 |
array([[1, 0, 0], [2, 3, 4]]) |
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.