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

Numpy – How can I transform this array without using python loops?

My input is a list of y_true labels, where the element in position i contains a value in the range of 0..len(classes) and depicts what class that element of the data set truly is. i ranges from 0 to len(data). Example below:

# 5 elements in data, 3 classes, all of which had representation in the data:
y_true = [0,2,1,0,1]

I want my output to be a list of lists that islen(data) by len(classes), where inner list i would have a 1 in the position of y_true[i], and 0 in the other len(classes)-1 slots, example:

#same configuration as the previous example
y_true = [0,2,1,0,1]  
result = [[1,0,0],[0,0,2],[0,1,0],[1,0,0],[0,1,0]]

Here’s how I’m initilazing result:

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

result = np.zeros((len(y_true), max(y_true)+1))

However I haven’t been able to make any further progress with this issue. I tried using add.at(result, y_true, 1) and this with y_true’s shape flipped, but neither produced the result I wanted. What fuction(s) can achieve what I’m trying to do here?

Edit: For better clarity on what I want to achieve, I made it using a for loop:

result = np.zeros((len(y_true), max(y_true)+1))
for x in range(4):
  result[x][y_true[x]] = 1

>Solution :

You can use fancy indexing:

result = np.zeros((len(y_true), max(y_true)+1), dtype=int)
result[np.arange(len(y_true)), y_true] = 1

output:

array([[1, 0, 0],
       [0, 0, 1],
       [0, 1, 0],
       [1, 0, 0],
       [0, 1, 0]])

alternative

an interesting alternative might be to use pandas.get_dummies:

import pandas as pd
result = pd.get_dummies(y_true).to_numpy()

output:

array([[1, 0, 0],
       [0, 0, 1],
       [0, 1, 0],
       [1, 0, 0],
       [0, 1, 0]], dtype=uint8)
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