简体   繁体   中英

How to create a 3D array in Python with Numpy?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM