unique
and other set logic¶
NumPy has some basic set operations for one-dimensional ndarray
. A commonly used one is numpy.unique, which returns the sorted unique values in an array:
[1]:
import numpy as np
names = np.array(
[
"Liam",
"Olivia",
"Noah",
"Liam",
"Noah",
"Olivia",
"Liam",
"Emma",
"Oliver",
"Ava",
]
)
[2]:
np.unique(names)
[2]:
array(['Ava', 'Emma', 'Liam', 'Noah', 'Oliver', 'Olivia'], dtype='<U6')
With numpy.in1d you can check the membership of the values in a one-dimensional array to another array and a boolean array is returned:
[3]:
np.isin(names, ["Emma", "Ava", "Charlotte"])
[3]:
array([False, False, False, False, False, False, False, True, False,
True])
Array set operations:
Method |
Description |
---|---|
|
calculates the sorted, unique elements in |
|
calculates the sorted common elements |
|
calculates the sorted union of elements |
|
computes a boolean array indicating whether each element of |
|
sets the difference of the elements in |
|
sets symmetric differences; elements contained in one of the arrays but not in both |