np_utils.to_categorical Reverse np_utils.to_categorical Reverse numpy numpy

np_utils.to_categorical Reverse


You can use np.argmax to retrieve back those ids and then simply indexing into uniques should give you the original array. Thus, we would have an implementation, like so -

uniques[y_code.argmax(1)]

Sample run -

In [44]: arrOut[44]: array([5, 7, 3, 2, 4, 3, 7])In [45]: uniques, ids = np.unique(arr, return_inverse=True)In [46]: y_code = np_utils.to_categorical(ids, len(uniques))In [47]: uniques[y_code.argmax(1)]Out[47]: array([5, 7, 3, 2, 4, 3, 7])