Contenido del Curso
NumPy in a Nutshell
NumPy in a Nutshell
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)
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!
Swipe to show code editor
- You need to create two arrays:
- the first one is a 2-D array containing two arrays with the values:
1, 5, 2
and34, 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
and6, 1, 9
.
- the first one is a 2-D array containing two arrays with the values:
- Display these arrays on the screen: first
arr_1
, thenarr_2
.
¡Gracias por tus comentarios!
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)
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!
Swipe to show code editor
- You need to create two arrays:
- the first one is a 2-D array containing two arrays with the values:
1, 5, 2
and34, 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
and6, 1, 9
.
- the first one is a 2-D array containing two arrays with the values:
- Display these arrays on the screen: first
arr_1
, thenarr_2
.
¡Gracias por tus comentarios!