# [Numpy-discussion] converting discrete data to unique integers

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

```Hi,

Suppose I have an array 'd'

In [75]: d
Out[75]:
array(['parrot', 'parrot', 'dog', 'cat', 'parrot', 'dog', 'parrot',
'cat',
'dog', 'dog', 'dog', 'cat', 'cat', 'dog', 'cat', 'parrot',
'cat',
'cat', 'dog', 'parrot', 'parrot', 'parrot', 'cat', 'dog',
'parrot',
'dog', 'dog', 'dog', 'dog', 'parrot', 'parrot', 'cat', 'dog',
'parrot', 'cat', 'parrot', 'cat', 'dog', 'parrot', 'cat',
'parrot',
'cat', 'parrot', 'parrot', 'parrot', 'parrot', 'dog', 'cat',
'parrot', 'cat'],
dtype='|S6')

I'd like to map every unique element (these could be strings, objects,
or already ints) to a unique integer between 0 and len(unique(d)) - 1.

The solution I've come up with is

In [76]: uniqueind, vectorind = (d == unique(d)[:, newaxis]).nonzero()

In [77]: myints = uniqueind[argsort(vectorind)]

But I wonder if there's a better way to do this. Anyone ever run into
this problem before?

David
```

More information about the NumPy-Discussion mailing list