A dimension in arrays refers to one level of depth in nested arrays.
Nested arrays are arrays that contain other arrays as their elements. |
0-D arrays, also known as scalars, represent the individual elements within an array. Each value in an array is a 0-D array.
Create a 0-D array with the value 42.
import numpy as np arr = np.array(42) print(arr) |
An array composed of 0-D arrays as its elements is referred to as a one-dimensional (1-D) array. These are the most fundamental and widely used type of arrays.
Construct a one-dimensional (1-D) array with the values 1, 2, 3, 4, and 5.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) |
An array consisting of one-dimensional (1-D) arrays as its elements is called a two-dimensional (2-D) array. These are commonly used to represent matrices or second-order tensors.
NumPy includes a dedicated submodule for matrix operations known as numpy.mat. |
Create a two-dimensional (2-D) array that consists of two arrays: the first with the values 1, 2, 3, and the second with the values 4, 5, 6.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr) |
An array that contains two-dimensional arrays (matrices) as its elements is called a three-dimensional (3-D) array. These are often used to represent third-order tensors.
Create a three-dimensional (3-D) array consisting of two two-dimensional (2-D) arrays, where each 2-D array contains two arrays with the values 1, 2, 3 and 4, 5, 6.
import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) print(arr) |