After predicting a certain image I got the following classes:
np.argmax(classes, axis=2)
array([[ 1, 10, 27, 8, 2, 6, 6]])
I now want to translate the classes to the corresponding letters numbers. To onehot encode my classes before I used this code (in order to see which class stands for which letter/number:
def my_onehot_encoded(label):
# define universe of possible input values
characters = '0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ'
# define a mapping of chars to integers
char_to_int = dict((c, i) for i, c in enumerate(characters))
int_to_char = dict((i, c) for i, c in enumerate(characters))
# integer encode input data
integer_encoded = [char_to_int[char] for char in label]
# one hot encode
onehot_encoded = list()
for value in integer_encoded:
character = [0 for _ in range(len(characters))]
character[value] = 1
onehot_encoded.append(character)
return onehot_encoded
That means: class 1
is equal to number 1
, class 10
to A
and so on. How can I invert this and get the array to a new label?
Thanks a lot in advance.
CodePudding user response:
Not sure I understand the problem, but this might work?
import numpy as np
a = np.array([[ 1, 10, 27, 8, 2, 6, 6]])
characters = '0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ'
np.array(list(characters))[a]
output:
array([['1', 'A', 'S', '8', '2', '6', '6']], dtype='<U1')
If you want it as a string:
"".join(np.array(list(characters))[a].flat)
output:
'1AS8266'
CodePudding user response:
def my_onehot_encoded(classes_array):
# define universe of possible input values
characters = '0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ'
return "".join([characters[c] for c in classes_array])
print(my_onehot_encoded([1, 11, 20]))
I got the following output:
1BK