Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I convert a matrix into characters?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading