Dimensions of Array

M3-R5.1 · Chapter 9: NumPy Basics · 2 min read

Array Dimensions

A dimension in NumPy is one level of array depth.

Arrays inside arrays are called nested arrays.

Types of Arrays

0-D Array (Scalar)

A single value — the most basic element in NumPy.

import numpy as np

arr = np.array(42)

print(arr)          # 42

print(arr.ndim)     # 0

1-D Array (Unidimensional)

1D array Contains 0-D array as Elements .

Most common type.

arr = np.array([1, 2, 3, 4, 5])

print(arr.ndim)     # 1

2-D Array (Matrix )

2D array Has 1-D arrays as its elements. Used for matrices.

NumPy provides numpy.mat sub-module for matrix operations.

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr.ndim)     # 2

3-D Array (3rd Order Tensor)

Has 2-D arrays as its elements.

arr = np.array([[[1,2,3],[4,5,6]], [[1,2,3],[4,5,6]]])

print(arr.ndim)     # 3

Higher-Dimensional Arrays

Use the ndmin argument to set any number of dimensions at creation time.

arr = np.array([1, 2, 3, 4], ndmin=5)

print(arr.ndim)     # 5

Note: The ndim attribute returns an integer showing how many dimensions an array has.

Note : 0-D = scalar, 1-D = vector, 2-D = matrix, 3-D = tensor. Know this for MCQs.