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

unique(x)

calculates the sorted, unique elements in x

intersect1d(x, y)

calculates the sorted common elements x and y

union1d(x, y)

calculates the sorted union of elements

in1d(x, y)

computes a boolean array indicating whether each element of x is contained in y

setdiff1d(x, y)

sets the difference of the elements in x that are not contained in y

setxor1d(x, y)

sets symmetric differences; elements contained in one of the arrays but not in both