I have labels, where it's shape is [lb,sz], for example here [3,2].
We have also targets, where it is a 1d array (size [bz]), use to select the labels.
How to select the labels based on targets, creating a new array such that its shape is [bz,lb,sz], such that all except the targets are zeros. Look at the code snippet for example.
Example for clarity on the problem
labels = np.array([[1,1],[2,2],[3,3]])
print("labels", labels.shape) # labels (3, 2)
targets = np.array([0,1,1,0,2])
print("targets", targets.shape) # targets (5,)
target_labels = labels[targets]
print(target_labels)
# [[1 1]
# [2 2]
# [2 2]
# [1 1]
# [3 3]]
# TODO: need help here!
try_target_labels = np.zeros((targets.shape[0],labels.shape[0],labels.shape[1]))
try_target_labels[:,0,:] = target_labels # TODO: how to replace `0` to select based on `targets`?
print(try_target_labels)
# [[[1. 1.]
# [0. 0.]
# [0. 0.]]
# [[2. 2.]
# [0. 0.]
# [0. 0.]]
# [[2. 2.]
# [0. 0.]
# [0. 0.]]
# [[1. 1.]
# [0. 0.]
# [0. 0.]]
# [[3. 3.]
# [0. 0.]
# [0. 0.]]]
# ideal outcome
ideal_target_labels = np.array([[[1,1],[0,0],[0,0]], [[0,0],[2,2],[0,0]], [[0,0],[2,2],[0,0]], [[1,1],[0,0],[0,0]], [[0,0],[0,0],[3,3]]])
print("ideal_target_labels", ideal_target_labels.shape) # ideal_target_labels (5, 3, 2)
print(ideal_target_labels)
# [[[1 1]
# [0 0]
# [0 0]]
# [[0 0]
# [2 2]
# [0 0]]
# [[0 0]
# [2 2]
# [0 0]]
# [[1 1]
# [0 0]
# [0 0]]
# [[0 0]
# [0 0]
# [3 3]]]
p.s. I hope that the code snippet is clear, any help to rephrase the title or the questions are welcome too.
Edit: partial correct implementation achieved, but problem when rows shared the same value
Thanks to @gofvonx contribution, np.where(target_labels[:, None, :]==labels[None, :, :], target_labels[:, None, :], 0) was able to get the right solution based on the example above. But the actual data, do occasionally share a few values that are the same. Look at the code snippet for example.
labels = np.array([[1,2],[1,3],[1,4]])
print("labels", labels.shape) # labels (3, 2)
targets = np.array([0,1,1,0,2])
print("targets", targets.shape) # targets (5,)
target_labels = labels[targets]
print("target_labels", target_labels)
# [[1 2]
# [1 3]
# [1 3]
# [1 2]
# [1 4]]
# TODO: need help here!
try_target_labels = np.zeros((targets.shape[0],labels.shape[0],labels.shape[1]))
try_target_labels = np.where(target_labels[:, None, :]==labels[None, :, :], target_labels[:, None, :], 0) # TODO: need help here!
print(try_target_labels)
# [[[1 2]
# [1 0]
# [1 0]]
# [[1 0]
# [1 3]
# [1 0]]
# [[1 0]
# [1 3]
# [1 0]]
# [[1 2]
# [1 0]
# [1 0]]
# [[1 0]
# [1 0]
# [1 4]]]
# ideal outcome
ideal_target_labels = np.array([[[1,2],[0,0],[0,0]], [[0,0],[1,3],[0,0]], [[0,0],[1,3],[0,0]], [[1,2],[0,0],[0,0]], [[0,0],[0,0],[1,4]]])
print("ideal_target_labels", ideal_target_labels.shape) # ideal_target_labels (5, 3, 2)
print(ideal_target_labels)
# [[[1 2]
# [0 0]
# [0 0]]
# [[0 0]
# [1 3]
# [0 0]]
# [[0 0]
# [1 3]
# [0 0]]
# [[1 2]
# [0 0]
# [0 0]]
# [[0 0]
# [0 0]
# [1 4]]]
np.zeros((4,3,2),int)and copying thetarget_labelsrows to the correct slots.# try_target_labels = np.zeros((5,3,2)) # try_target_labels[:,targets] = target_labelsand other permutations. I think something like,try_target_labels[:,0] = target_labels, but i need a way to replace that0.