Python NumPy For Your Grandma | Section 4.7 | unique()


Course Contents

  1. Introduction
  2. 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
  3. Intermediate Array Stuff
    3.1 Broadcasting
    3.2 newaxis
    3.3 reshape
    3.4 boolean indexing
    3.5 nan
    3.6 infinity
    3.7 random
  4. Common Operations
    4.1 where
    4.2 Math Funcs
    4.3 all and any
    4.4 concatenate
    4.5 Stacking
    4.6 Sorting
    4.7 unique
  5. Challenges

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