[Numpy-discussion] converting discrete data to unique integers

David Warde-Farley dwf@cs.toronto....
Wed Nov 4 14:57:19 CST 2009


Thanks Alan and Robert, I probably should have mentioned that I was  
interested in obtaining the corresponding integer for each value in  
the array d, in which case the dictionary bit works but would require  
a further loop to expand.

On 4-Nov-09, at 3:22 PM, Robert Kern wrote:

> I'd toss in a sorted() just to keep the mapping stable.

Good point. With the return_inverse solution, is unique() guaranteed  
to give back the same array of unique values in the same (presumably  
sorted) order? That is, for two arrays A and B which have elements  
only drawn from a set S, is all(unique(A) == unique(B)) guaranteed?  
The code is a quite clever and a bit hard to follow, but it *looks*  
like it will provide a stable mapping since it's using a sort.

David


More information about the NumPy-Discussion mailing list