I want to create a 3-D np.array
of dimensions (100x100x100)
with the tuple (-1,-1,-1)
for every element. Is there a faster/better way than:
np.array([[[(0,0,0) for x in range(100)] for y in range(100)] for z in range(100)])
?
I want to create a 3-D np.array
of dimensions (100x100x100)
with the tuple (-1,-1,-1)
for every element. Is there a faster/better way than:
np.array([[[(0,0,0) for x in range(100)] for y in range(100)] for z in range(100)])
?
Your code is not really creating a (100x100x100) array.
Maybe you can try:
np.ones((100, 100, 100))*-1
As you can see the shape of your array is actually (100, 100, 100, 3)
>>> np.array([[[(0,0,0) for x in range(100)] for y in range(100)] for z in range(100)]).shape
(100, 100, 100, 3)
You can uses the np.full
function to generate the desired array.
np.full([100, 100, 100, 3], -1)
This creates a 100x100x100x3 numpy array, which has an equivalent structure to yours as required.
>>> (np.array([[[(0,0,0) for x in range(100)] for y in range(100)] for z in range(100)]) == np.full([100, 100, 100, 3], 0)).all()
True
Documentation for np.full()