I am trying create a 3D array with ones in the first n places of the 1 axis, where n comes from an array with a length of the 0 axis. I am working with a large dataset and trying to speed this up.
I think the code will make more sense. I am trying to vectorize the for loop.
import numpy as np
data = np.zeros((3, 4, 5))
num = np.array([2, 4, 3])
for i in range(len(num)):
data[:, 0][i, 0:num[i]] = 1
print(data)
This is the result I am looking for:
[[[1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
[[1. 1. 1. 1. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
[[1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]]