How to get index into unique values in numpy

Hi all. I am embarrassed to ask this here, but I just cannot figure it out.

Suppose we have a big numpy array

y_train
array([‘wesmea’, ‘whtspa’, ‘hoowar’, …, ‘gcrfin’, ‘boboli’, ‘buffle’],
dtype=’<U7’)

and a much smaller numpy array of the unique names in y_train

y_unique
array([‘aldfly’, ‘ameavo’, ‘amebit’, ‘amecro’, ‘amegfi’, ‘amekes’,
‘amepip’, ‘amered’, ‘amerob’…])

How do I derive the index of every element of y_train into y_unique? Like,
array([200, 201, 23, ...,27, 5, 6'])

Does this task really require a loop? Or a list comprehension outside of numpy?

Perplexed, Malcolm

P.S. I see np.unique with return_inverse, but in this situation y_unique contains more than the unique values in y_train.

Fastai uses a dictionary method :slight_smile: so this way you could have name2idx and idx2name (honestly super useful to know how to do these!). IIRC it’s kept in c2i in fastai so take a look at that method. (Find it via dls.c2i??, or dls.categorify.c2i??, not sure which OTTOMH)

1 Like