Skip to content

Instantly share code, notes, and snippets.

# Abbreviated way to create groups of (nearly) identical values.
import numpy as np
# Random example data.
a = np.array([11.1, 1, 1, 2, 3, 4, 5, 5, 1, 5, 5, 6, 6, 7, 9, 7, 8, 7, 10, 11.1, 11.2])
EPS = 1e-7 # matching precision
asorted = np.sort(a) # line up the values neatly, but don't alter
# Get all indices where a change in value occurs
indices = np.where(np.abs(np.diff(asorted)) > EPS)[0] + 1
# Group identical values