Skip to content

Instantly share code, notes, and snippets.

@alimuldal
Created October 20, 2015 00:14
Show Gist options
  • Save alimuldal/f900f2a3e676348bc34b to your computer and use it in GitHub Desktop.
Save alimuldal/f900f2a3e676348bc34b to your computer and use it in GitHub Desktop.
numpy unique rows
def unique_rows(a, **kwargs):
rowtype = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
b = np.ascontiguousarray(a).view(rowtype)
return_index = kwargs.pop('return_index', False)
out = np.unique(b, return_index=True, **kwargs)
idx = out[1]
uvals = a[idx]
if (not return_index) and (len(out) == 2):
return uvals
elif return_index:
return (uvals,) + out[1:]
else:
return (uvals,) + out[2:]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment