ndarray.ndim and Axes
The dimension of an array refers to the number of axes (or dimensions) in the array.
ndarray object has many attributes (discussed in the next section). One of these attributes is ndim (ndarray.ndim).
ndim attribute specifies the number of axes (dimensions) of the array. Each axis corresponds to a dimension in the array and is associated with a specific dimension of the array.
What are Axes in NumPy
Below are the axes of different NumPy arrays:
1D Array: A 1-D Array is a list of numbers. It has one axis.
Example 1: an array of shape (4,) has one axis with four elements. Example 2: an array of shape (3,) has one axis with three elements.
Note” Attribute is discussed in the next section.
A 2-D Array is a matrix of rows and columns. It has 2 axes.
For example, an array of shape (3, 4) has:
- Axis 0: Rows (3 elements)
- Axis 1: Columns (4 elements)
3-D Array: A 3-D array is a matrix of matrices. It has 3 axes. (Imagine a 3-D array as a cube with array elements along the x, y, and z co-ordinates).
For example, an array of shape (2, 3, 4) has:
- Axis 0: Depth (2 elements)
- Axis 1: Rows (3 elements)
- Axis 2: Columns (4 elements)
The Table below relates the array’s dimension to its axis.
Table of Axes for Different Types of Arrays
| Type of Array | Description | Example | Shape | Axes Count |
| Scalar | Single value | np.array(5) | () | 0 |
| 1D Array | One-dimensional array A list of numbers | np.array([1, 2, 3]) | (3,) | 1 |
| 2D Array | Two-dimensional array (matrix with rows and columns) | np.array([[1, 2], [3, 4]]) | (2, 2) | 2 |
| 3D Array | Three-dimensional array (collection of matrices) | np.array([[[1], [2]], [[3], [4]]]) | (2, 2, 1) | 3 |
Ndarray Attributes
NumPy ndarray has several attributes, such as, dtype, shape, size, and many more. A few of these attributes are listed below.
ndarray.ndim
The number of axes (dimensions) of the array. (see table below)
ndim tells us the dimensionality of the NumPy array: The number of dimensions can be thought of as the number of distinct “axes”.
ndarray.shape
Attribute shape returns a tuple containing the number of elements in each dimension.
This is a tuple of integers indicating the size of the array in each dimension. For a 2-D arrow with n rows and m columns, the shape will be (n,m). The length of the shape tuple is therefore the number of axes, ndim. For an array of shape (n,m), the number of axis is 2. (See table below)
ndarray.size
size tells us the total number of elements of the array. This is equal to the product of the elements of shape ( n*m). See table below.
ndarray.dtype
dtype object describes the type of the elements in the array. dtype refers to the type of the array elements.
NumPy provides several dtypes, in addition to the ones in Python. You can create arrays by specifying standard Python types or the NumPy dtypes.
Some of NumPy dtypes are listed below:
- numpy.int32
- numpy.int16
- numpy.float64
ndarray.itemsize
Attribute itemsize is the size of each element of the array in bytes.
For example, type numpy.float64 element has itemsize 8 (=64/8), while the type numpy.complex32 has itemsize 4 (=32/8).
It is equivalent to ndarray.dtype.itemsize.
The table below shows more details.
Table ndarray Attributes
| Array Type | Description | Array Example | # of Axis | Array.ndim | # of Elements Array.size | Array.shape |
| Scalar | 0-dimension | Single value np.array(5) | 0 | 0 | () | |
| 1-D Array | One-dimensional array | x= np.array1([1, 2, 6, 8]) print(x) [1 2 6 8] | 1 | array1.ndim 1 | array1.size 4 | array1.shape (4,) |
| 2-D Array | Two-dimensional array (matrix) | Y = np.array2d([[1, 2], [4, 9], [5, 6]]) Print(y) [[1 2] [4 9] [5 6]] | 2 | array2d.ndim 2 | array2d.size 6 | array2d.shape (3, 2) |
| 3-D Array | Three-dimensional array | Z=np.array3([[[1, 2], [3, 4], [10, 11]], [[5, 6], [8, 9], [12, 13]]]) Print(z) [[[ 1 2] [ 3 4] [10 11]] [[ 5 6] [ 8 9] [12 13]]] | 3 | array3.ndim 3 | array3.size 12 | array3.shape (2, 3, 2) |
Program Example: ndim, shape, size, dtype (1)
The following few program examples show how to find various ndray’s attributes
Program Example: ndim, shape, size, dtype (2)
Program Example: ndim, shape, size, dtype (3)
The following program prints the ndarray attributes for floting-point and complex numbers data types