Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert categorical data back to numbers using keras utils to_categorical

I am using to_categorical from keras.utils for one-hot encoding the numbers in a list. How can get back the numbers from categorical data? Is there any function available for that.

Y=to_categorical(y, num_classes=79)
like image 269
Shiva Ganesh Avatar asked Jan 11 '19 09:01

Shiva Ganesh


1 Answers

You can do it simply by np.argmax():

import numpy as np
y = [0, 1, 2, 0, 4, 5]
Y = to_categorical(y, num_classes=len(y))
print(Y)
y = np.argmax(Y, axis=-1)
print(y)
# [0, 1, 2, 0, 4, 5]

Why use argmax(axis=-1)?

In the above example, to_categorical returns a matrix with shape (6,6). Set axis=-1 means, extract largest indices in each row.

[[1. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0.]
 [1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 1.]]

See more at here about indexing.

What if my data have more than 1 dimension?

No difference. Each entry, in the preliminary list, converts to a one-hot encoding with the size of [1, nb_classes] which only one index is one and the rest are zero. Similar to the above example, when you find the maximum in each row, it converts to the original list.

y = [[0, 1], [2, 0], [4, 5]]
Y = keras.utils.to_categorical(y, num_classes=6)

#[[[1. 0. 0. 0. 0. 0.]
#  [0. 1. 0. 0. 0. 0.]]
#
# [[0. 0. 1. 0. 0. 0.]
#  [1. 0. 0. 0. 0. 0.]]
#
# [[0. 0. 0. 0. 1. 0.]
#  [0. 0. 0. 0. 0. 1.]]]

y = np.argmax(Y, axis=-1)

#[[0 1]
# [2 0]
# [4 5]]
like image 91
Amir Avatar answered Nov 14 '22 05:11

Amir