Python NumPy For Your Grandma | Section 4.7 | unique()
- NumPy Arrays
2.1 What’s A NumPy Array
2.2 Creating NumPy Arrays
2.3 Indexing And Modifying 1-D Arrays
2.4 Indexing And Modifying Multidimensional Arrays
2.5 Basic Math
- Intermediate Array Stuff
3.4 boolean indexing
- Common Operations
4.2 Math Funcs
4.3 all and any
This video covers NumPy’s unique() function, used for extracting the distinct values from an array.
import numpy as np foo = np.array(['b', 'b', 'a', 'a', 'c', 'c']) # get uniques np.unique(foo) # ['a', 'b', 'c', 'nan'] # get uniques sorted by first occurrence uniques, first_positions = np.unique(foo, return_index = True) uniques[np.argsort(first_positions)] # ['b', 'a', 'c'] # get uniques with counts np.unique(foo, return_counts=True)
Numpy provides a unique() function for extracting the unique elements of an array.
It’s pretty simple to use. If you have an array called gar then unique(gar) gives you the unique elements.
Simple enough. …although, you may have noticed that the order in which the elements occurred in the original array is different than the output. In the original array, b occurred first but in the output, a occurs first. That’s because unique() returns the unique elements in sorted order.
Let’s try that again, but this time we’ll call unique() with return_index = True.
With return_index = True, numpy gives us a tuple containing
- the unique elements array and
- a corresponding array that tells us the index at which each element first occurred in the original array.
For example, ‘a’ first occurred at index 2 in the original array, b first occurred at index 0, and so on. This is useful if you want to reorder the unique elements in the same order they occurred in the original array. To do this, argsort() the index array and use that to sort the unique array.
Another sweet feature of unique() is that you can tell numpy to return the count of each element. To do this, just set return_counts = True