0

I want to create a 2x2x3 three-dimensional array in Python.

I did the following:

x = numpy.array([[[1,1], [2,2], [3,3]], [[1,1], [1,1], [1,1]]])

However, my output is a 2x3x2 array.

print(x.shape)

What did I wrong? And can anyone please explain step by step how to build a 3D array, I am a bit confused about the rows, colums and axis.

Thank you in advance.

1
  • 3
    If you do numpy.zeros((2,2,3)) you will see what an array of the desired shape (filled with zeros, obviously) looks like. Commented Jul 4, 2020 at 22:06

1 Answer 1

3

This might be what you want

x = np.array([[[1,1,2], [2,3,3]], [[1,1,1], [1,1,1]]])
print(x.shape)
print(x)

Refer to this documentation for explanation.

What you created was an array with 3 rows, 2 columns and say 2 frames so you didn't get what you wanted (2 rows & 3 columns). We can make a 3d array representation as (frames, rows, columns).

Further you could've created an array with dimensions (n,) using

x = np.array([1, 1, 2, 2, 3, 3, 1, 1, 1, 1, 1, 1])

Then you can reshape it as per the requirement

For 2x2x3 you could do

x = x.reshape(2,2,3)

Similarly for 2x3x2

x = x.reshape(2,3,2)
Sign up to request clarification or add additional context in comments.

3 Comments

This works :), Can you maybe explain your solution further? Or do you have tips/sources that explains this?
@Tom I've made edit with an explanation. Hope it helps!

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.