Extract unique rows from a matrix in numpy with the frequency of each row that was created
Question: A follow up question on:

[How can i use the unique(a, 'rows') from MATLab at
python?](http://stackoverflow.com/questions/6776821/how-can-i-use-the-uniquea-
rows-from-matlab-at-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!


Answer: 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]))
    

