A follow up question on:
How can I use the unique(a, 'rows') from MATLAB in Python?
The answer there explains how to get the unique rows. Yet matlab also returns the frequency of each row that was created. Any elegant way to make it with python?
Thanks!
You can count the number of each unique row using fancy indexing and evaluating a condition like:
from numpy import unique, array, all
def myunique(input):
u = array([array(x) for x in set(tuple(x) for x in input)])
return u, array([len(input[all(input==x, axis=1)]) for x in u],dtype=int)
example:
a = array([list('1234'),
list('1234'),
list('1222'),
list('1222'),
list('1234')],dtype=str)
print myunique(a)
#(array([['1', '2', '2', '2'],
# ['1', '2', '3', '4']],
# dtype='|S1'), array([2, 3]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With