1

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]]]
2
  • Consider making a np.zeros((4,3,2),int) and copying the target_labels rows to the correct slots. Commented Apr 15, 2021 at 2:50
  • i tried # try_target_labels = np.zeros((5,3,2)) # try_target_labels[:,targets] = target_labels and other permutations. I think something like, try_target_labels[:,0] = target_labels, but i need a way to replace that 0. Commented Apr 15, 2021 at 3:53

1 Answer 1

1

You can try to do a boolean comparison with appropriate broadcasting between target_labels and labels. This yields a boolean index for filling either target_labels or 0 using np.where:

np.where(np.all(target_labels[:, None, :]==labels[None, :, :], axis=-1)[:, :, None], target_labels[:, None, :], 0)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @gofvonx, I'm very thankful for your answer, I realized that there is an issue when labels has similar values. Look at my updated question, let me know if there is anything that's unclear, appreciate all the help that I can get.
@Jingles Good point! I have updated my answer. It now includes a check that the full sub-array is identical using np.all along the last axis. It is similar to my answer posted here regarding checking for the existence of sub-arrays in another array: stackoverflow.com/questions/66672342/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.