Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.unique with order preserved

Tags:

python

numpy

['b','b','b','a','a','c','c'] 

numpy.unique gives

['a','b','c'] 

How can I get the original order preserved

['b','a','c'] 

Great answers. Bonus question. Why do none of these methods work with this dataset? http://www.uploadmb.com/dw.php?id=1364341573 Here's the question numpy sort wierd behavior

like image 887
siamii Avatar asked Mar 26 '13 12:03

siamii


2 Answers

unique() is slow, O(Nlog(N)), but you can do this by following code:

import numpy as np a = np.array(['b','a','b','b','d','a','a','c','c']) _, idx = np.unique(a, return_index=True) print(a[np.sort(idx)]) 

output:

['b' 'a' 'd' 'c'] 

Pandas.unique() is much faster for big array O(N):

import pandas as pd  a = np.random.randint(0, 1000, 10000) %timeit np.unique(a) %timeit pd.unique(a)  1000 loops, best of 3: 644 us per loop 10000 loops, best of 3: 144 us per loop 
like image 179
HYRY Avatar answered Oct 08 '22 15:10

HYRY


Use the return_index functionality of np.unique. That returns the indices at which the elements first occurred in the input. Then argsort those indices.

>>> u, ind = np.unique(['b','b','b','a','a','c','c'], return_index=True) >>> u[np.argsort(ind)] array(['b', 'a', 'c'],        dtype='|S1') 
like image 35
Fred Foo Avatar answered Oct 08 '22 16:10

Fred Foo