Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract unique rows from a matrix in numpy with the frequency of each row that was created

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!

like image 703
Yuval Atzmon Avatar asked Nov 12 '22 03:11

Yuval Atzmon


1 Answers

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]))
like image 62
Saullo G. P. Castro Avatar answered Nov 14 '22 23:11

Saullo G. P. Castro