Maping a list to new list

Hi everyone,

I have ground_truth which is numpy list and is hot vector list of actions.

array([[0. , 1. , 0. ],
       [0. , 1. , 0. ],
       [0. , 1. , 0. ],
       ...,
       [0. , 0. , 0.8],
       [0. , 0. , 0.8],
       [0. , 0. , 0.8]], dtype=float32)

I want to map it like

{  '[-1.0, 0.0, 0.0]' :  '[1.0, 0.0, 0.0, 0.0]',
   '[+1.0, 0.0, 0.0]' : '[0.0, 1.0, 0.0, 0.0]',
   '[0.0, +1.0, 0.0]' : '[0.0, 0.0, 1.0, 0.0]',
   '[0.0, 0.0, 0.8]' :   '[0.0, 0.0, 0.0, 1.0]'  }

I did this loop below to make it but in some cases although my ground truth is sth else, but it gives me like [0.0, 0.0, 0.0, 0.0]. Would you please help me about it.

my already code is:

new_ground_truth=[]
z = [0 , 0 , 0]
for g in range (len(ground_truth)):
    blank = np.zeros(4)
    if(not(ground_truth[g] == z).all()):
        if(ground_truth[g][0]== -1): blank[0] = 1  
        if(ground_truth[g][0]== 1) : blank[1] = 1 
        if(ground_truth[g][1]== 1) : blank[2] = 1  
        if(ground_truth[g][2]==0.8): blank[3] = 1 
    else:
        None
    new_ground_truth.append(np.asarray(blank))

one of test cases which gives me wrong value:

ground_truth[4989] = array([0. , 0. , 0.8], dtype=float32)
but
new_ground_truth[4989]= array([0., 0., 0., 0.])

Hey, I have a faster solution.

import numpy as np

gt = np.array([
    [-1.0, 0.0, 0.0],
    [1.0, 0.0, 0.0],
    [0.0, 1.0, 0.0],
    [0.0, 0.0, 0.8],
    [1.0, 0.0, 0.0]
])

new_gt = np.zeros((gt.shape[0], 4), dtype=gt.dtype)
new_gt[:, 1:] = gt > 0
new_gt[:, 0] = gt[:, 0] == -1
print(new_gt)

Addition:
new_gt = new_gt[gt.sum(axis=1) != 0, :] will filter out [0., 0., 0.] in gt

1 Like