Vectorized relabeling of NumPy array to consecutiv

2020-02-06 12:57发布

问题:

I have a huge training dataset with 4 classes. These classes are labeled non-consecutively. To be able to apply a sequential neural network the classes have to be relabeled so that the unique values in the classes are consecutive. In addition, at the end of the script I have to relabel them back to their old values.

I know how to relabel them with loops:

def relabel(old_classes, new_classes):
    indexes=[np.where(old_classes ==np.unique(old_classes)[i]) for i in range(len(new_classes))]
    for i in range(len(new_classes )):
        old_classes [indexes[i]]=new_classes[i]
    return old_classes

>>> old_classes = np.array([0,1,2,6,6,2,6,1,1,0])
>>> new_classes = np.arange(len(np.unique(old_classes)))
>>> relabel(old_classes,new_classes)
array([0, 1, 2, 3, 3, 2, 3, 1, 1, 0])

But this isn't nice coding and it takes quite a lot of time.

Any idea how to vectorize this relabeling?


To be clear, I also want to be able to relabel them back to their old values:

>>> relabeled_classes=np.array([0, 1, 2, 3, 3, 2, 3, 1, 1, 0])
>>> old_classes = np.array([0,1,2,6])
>>> relabel(relabeled_classes,old_classes )
array([0,1,2,6,6,2,6,1,1,0])

回答1:

We can use the optional argument return_inverse with np.unique to get those unique sequential IDs/tags, like so -

unq_arr, unq_tags = np.unique(old_classes,return_inverse=1)

Index into unq_arr with unq_tags to retrieve back -

old_classes_retrieved = unq_arr[unq_tags] 

Sample run -

In [69]: old_classes = np.array([0,1,2,6,6,2,6,1,1,0])

In [70]: unq_arr, unq_tags = np.unique(old_classes,return_inverse=1)

In [71]: unq_arr
Out[71]: array([0, 1, 2, 6])

In [72]: unq_tags
Out[72]: array([0, 1, 2, 3, 3, 2, 3, 1, 1, 0])

In [73]: old_classes_retrieved = unq_arr[unq_tags]

In [74]: old_classes_retrieved
Out[74]: array([0, 1, 2, 6, 6, 2, 6, 1, 1, 0])