0

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)])

?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Does this answer your question? [NumPy array initialization (fill with identical values)](https://stackoverflow.com/questions/5891410/numpy-array-initialization-fill-with-identical-values) – Tomerikoo Nov 18 '20 at 18:14
  • Explain why you need an array with tuple elements. That's not an optimal array computaitonally. Also tell us about the array your code produced. What's its `shape` and `dtype`. Is that really an array of tuples? – hpaulj Nov 18 '20 at 18:33

2 Answers2

0

Your code is not really creating a (100x100x100) array.

Maybe you can try:

np.ones((100, 100, 100))*-1
Felix Tang
  • 62
  • 7
  • This is not the shape that the OP is after, their array has shape (100, 100, 100, 3) – Tom Nov 18 '20 at 18:05
0

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()

Tom
  • 918
  • 6
  • 19