Skip to content

Instantly share code, notes, and snippets.

@evertrol
Created February 3, 2014 14:08
Show Gist options
  • Save evertrol/8784448 to your computer and use it in GitHub Desktop.
Save evertrol/8784448 to your computer and use it in GitHub Desktop.
# 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
groups = np.array([asorted[i1:i2] for i1, i2 in zip(np.concatenate(([0], indices)), np.concatenate((indices, [None])))])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment