classes = ['A', 'B', 'C']
my_data = [
[2, 1, 3],
[1, 1, 2],
[3, 3, 3],
[3, 1, 3],
[3, 1, 3],
[3, 3, 2]
]
Here, A = 1, B=2, and C=3.
Suppose, I want to first find the maximum value in each row of the matrix my_data, and then I want to convert them into characters from classes.
Can I do it in python without using loops?
The following source code is not working for me:
def prediction_to_name(pred):
return classes[np.argmax(pred)]
>Solution :
You need to iterate over your data unless repeatedly cut and paste result.append(classes[np.argmax(my_data[n])]) for each n in 0:len(my_data) which is just manually typing out the loop.
import numpy as np
classes = ['A', 'B', 'C']
my_data = [[2, 1, 3],
[1, 1, 2],
[3, 3, 3],
[3, 1, 3],
[3, 1, 3],
[3, 3, 2]]
classifiedData = [classes[np.argmax(row)] for row in my_data]
print(classifiedData) # ['C', 'C', 'A', 'A', 'A', 'A']