[Numpy-discussion] Finding unique rows in an array [Was: Finding a row match within a numpy array]

Mark.Miller mpmusu@cc.usu....
Wed Aug 29 13:05:09 CDT 2007


A belated thanks...but yes.  That does the trick.  I've not worked with 
views explicitly, so I appreciate the input.  I definitely foresee 
additional applications of these types of things in the future.

Thanks again,

-Mark

Francesc Altet wrote:
> 
> You can always do a view of the rows as strings and then use unique().
> Here is an example:
> 
> In [1]: import numpy
> In [2]: a=numpy.arange(12).reshape(4,3)
> In [3]: a[2]=(3,4,5)
> In [4]: a
> Out[4]: 
> array([[ 0,  1,  2],
>        [ 3,  4,  5],
>        [ 3,  4,  5],
>        [ 9, 10, 11]])
> 
> now, create the view and select the unique rows:
> 
> In [5]: b=numpy.unique(a.view('S%d'%a.itemsize*a.shape[0])).view('i4')
> 
> and finally restore the shape:
> 
> In [6]: b.reshape((len(b)/a.shape[1], a.shape[1]))
> Out[6]: 
> array([[ 0,  1,  2],
>        [ 3,  4,  5],
>        [ 9, 10, 11]])
> 
> If you want to find unique columns instead of rows, do a tranpose first 
> on the initial array.
> 
> Cheers,
> 


More information about the Numpy-discussion mailing list