What is a Dimension (Axis)?



ndim Attribute

Every NumPy array has an ndim attribute, which returns the number of dimensions (axes) of the array.



shape Attribute

The shape attribute returns a tuple of integers indicating the size of the array along each dimension.

The length of the shape tuple is equal to ndim.



Dimensions

0-D Array (Scalar)

A 0-dimensional array, also known as a scalar, is a single element. It has no axes.

import numpy as np
arr = np.array(42)
print(arr)
print("Dimension:", arr.ndim)

#Output - 
42  
Dimension: 0


Key Points :



Real-World Analogy :

Think of a 0-D array like a single coin — it’s not a row or table, just one item.



1-D Array (Vector)

A 1-D array (one-dimensional array) is like a simple list of values — a row of elements stored in a single line.

It has one axis.

import numpy as np

arr = np.array([10, 20, 30, 40, 50])
print(arr)
print("Dimension:", arr.ndim)

#Output - 
[10 20 30 40 50]  
Dimension: 1


Key Points :



Real-World Analogy :

A 1-D array is like a single row in Excel — just values from left to right.



2-D Array (Matrix)

A 2-D array is like a table — it has rows and columns. It’s the most commonly used array type in data science and machine learning.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
print("Dimension:", arr.ndim)

#Output - 
[[1 2 3]
 [4 5 6]]
Dimension: 2


Key Points :



Real-World Analogy :

Think of a 2-D array like an Excel sheet with rows and columns filled with numbers.



3-D Array (Tensor)

A 3-D array is like a collection of 2-D arrays — imagine a cube or a stack of tables, one on top of the other.

import numpy as np

arr = np.array([
    [[1, 2, 3], [4, 5, 6]],
    [[7, 8, 9], [10, 11, 12]]
])
print(arr)
print("Dimension:", arr.ndim)

#Output - 
[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]
Dimension: 3


Key Points :

1. It has 3 axes :

  1. Axis 0: depth (2 blocks here)
  2. Axis 1: rows
  3. Axis 2: columns

2. Shape = (2, 2, 3) → 2 blocks, each with 2 rows and 3 columns.

Useful for representing :



Real-World Analogy :

Imagine a book with 2 pages, and each page has a table (2 rows × 3 columns).



n-D Array (n-Dimensional Array)

An n-D array is a general term for arrays with any number of dimensions (1D, 2D, 3D, 4D, ...).

In NumPy, you can create arrays with n dimensions, making it powerful for complex data like images, videos, simulations, and deep learning.



Example : Creating an n-D Array (4D)

import numpy as np
# 4-D array: shape (2, 2, 2, 3)
arr = np.array([[[[1, 2, 3], [4, 5, 6]],
                 [[7, 8, 9], [10, 11, 12]]],

                [[[13, 14, 15], [16, 17, 18]],
                 [[19, 20, 21], [22, 23, 24]]]])

print("Array:")
print(arr)
print("\nDimensions:", arr.ndim)
print("Shape:", arr.shape)

#Output - 
Array:
[[[[ 1  2  3]
   [ 4  5  6]]

  [[ 7  8  9]
   [10 11 12]]]


 [[[13 14 15]
   [16 17 18]]

  [[19 20 21]
   [22 23 24]]]]

Dimensions: 4
Shape: (2, 2, 2, 3)


Key Points :

1. ndim : Returns number of dimensions

2. shape : Tuple of size in each dimension



Real-Life Use Cases of n-D Arrays :


n-D Example Use Case
1D Audio signals, simple lists
2D Grayscale images, Excel sheets
3D RGB Images, MRI scans
4D Videos (frames × channels × width × height)
5D+ Deep learning (e.g., batch processing of images)


1D vs 2D vs 3D NumPy Arrays :


Feature 1D Array 2D Array 3D Array
Structure Single row (like a list) Rows and columns (like a table) Stack of 2D arrays (like a cube)
Shape Example (5,) (2, 3) (2, 2, 3)
Dimensions (ndim) 1 2 3
Axis Info 1 axis 2 axes (rows, columns) 3 axes (depth, rows, columns)
Example [10, 20, 30, 40, 50] [[1, 2, 3], [4, 5, 6]] [[[1,2,3],[4,5,6]], [[7,8,9],[10,11,12]]]
Use Case Simple data (marks, age) Matrices, spreadsheets, grayscale images RGB images, video data, deep learning


Summary

1. Use 1D for simple sequences.

2. Use 2D for tabular or matrix data.

3. Use 3D for stacking matrices (like images, RGB, etc.).