Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
3-D Arrays | Dimensions in Arrays
NumPy in a Nutshell

book
3-D Arrays

With three-dimensional arrays, everything is quite clear and logical. These arrays consist of elements that are two-dimensional arrays.

Let's practice to make it easier to understand.

Here's an example of how we can create a 3-D array with four 2-D arrays, each containing three 1D arrays with 2 elements:

import numpy as np
# Creating array
arr = np.array([
[
[1, 2], [4, 3], [7, 4]
],
[
[2, 10], [9, 15], [7, 5]
],
[
[1, 11], [3, 20], [0, 2]
],
[
[9, 25], [6, 13], [9, 8]
]
])
# Displaying array
print(arr)
123456789101112131415161718
import numpy as np # Creating array arr = np.array([ [ [1, 2], [4, 3], [7, 4] ], [ [2, 10], [9, 15], [7, 5] ], [ [1, 11], [3, 20], [0, 2] ], [ [9, 25], [6, 13], [9, 8] ] ]) # Displaying array print(arr)
copy

Let's now take a look at the visualization of this array:

Our 3D array is 4x3x2, hence why we have a rectangular parallelepiped with sides equal to 4, 3 and 2, respectively. The innermost 1D arrays lie along the axis 2 (e.g., [1, 2] or [4, 3]) where each small cube with side equal to 1 is a particular element (number).

Basically, all the elements of a 3D array are stored inside these innermost 1D arrays. The rectangular parallelepiped is just a visual representation for us to make things clear. The total number of elements (small cubes) is equal to 24 (the volume of the rectangular parallelepiped), which 4 * 3 * 2.

Note

Since its a 2D visualization of a 3D object, we cannot display and see here all the elements.

Time to test your strength!

Tarea
test

Swipe to show code editor

  1. You need to create two arrays:
    • the first one is a 2-D array containing two arrays with the values: 1, 5, 2 and 34, 2, 7;
    • the second one is a 3-D array (use only a single line to create this array) containing two 2-D arrays, both of which include two arrays with the values 5, 3, 8 and 6, 1, 9.
  2. Display these arrays on the screen: first arr_1, then arr_2.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
import numpy as np

# 1. Creating arrays [[1, 5, 2], [34, 2, 7]] and [[[5, 3, 8], [6, 1, 9]], [[5, 3, 8], [6, 1, 9]]]
arr_1 = ___
arr_2 = ___

# 2. Display the first array
print(___)
print('-' * 13)

# 3. Display the second array
print(___)
toggle bottom row
We use cookies to make your experience better!
some-alt