Array Shapes
The shape of a NumPy array tells us how many dimensions it has and how many elements exist along each dimension. Understanding shape is essential before working with Machine Learning and Deep Learning data.
Shape tells you the structure of an array.
For example, an array with 2 rows and 3 columns has a shape of (2, 3). Think of shape as describing the size of the array in each dimension.
What Is Array Shape?
The shape of a NumPy array describes the size of the array along each dimension.
For example:
import numpy as np numbers = np.array([10, 20, 30, 40, 50]) print(numbers.shape)
(5,)
The result (5,) means the array contains
5 elements in one dimension.
Shape of a 1D Array
Consider this array:
numbers = np.array([10, 20, 30, 40, 50])
Visually, it looks like this:
10 20 30 40 50
It contains 5 values, so:
print(numbers.shape)
(5,)
This is a one-dimensional array.
Shape of a 2D Array
Now consider an array containing rows and columns:
data = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(data)
[[10 20 30] [40 50 60]]
Let's count:
Rows = 2 Columns = 3
Therefore the shape is:
print(data.shape)
(2, 3)
Read this as: 2 rows and 3 columns.
How to Read a Shape
This is one of the most important things to understand.
shape = (2, 3)
For a 2D array:
(rows, columns) (2, 3) ↑ ↑ │ └── 3 columns └────── 2 rows
So:
(2, 3) → 2 rows → 3 columns
More Shape Examples
| Array | Shape | Meaning |
|---|---|---|
| [1, 2, 3] | (3,) | 3 elements in 1 dimension |
| [[1, 2], [3, 4]] | (2, 2) | 2 rows and 2 columns |
| [[1, 2, 3], [4, 5, 6]] | (2, 3) | 2 rows and 3 columns |
| [[1], [2], [3], [4]] | (4, 1) | 4 rows and 1 column |
Using the shape Property
NumPy arrays have a built-in property called
shape.
import numpy as np
data = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(data.shape)
(2, 3)
Notice that we use:
data.shape
not:
data.shape()
shape is a property, not a function.
Shape and Dimensions
Shape also helps us understand how many dimensions an array has.
For example:
numbers = np.array([10, 20, 30, 40]) print(numbers.shape) print(numbers.ndim)
(4,) 1
Here:
shape = (4,) ndim = 1
So this is a one-dimensional array containing four elements.
Checking a 2D Array
data = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(data.shape)
print(data.ndim)
(2, 3) 2
This tells us:
shape → (2, 3)
2 rows
3 columns
ndim → 2 dimensions
Three-Dimensional Arrays
NumPy can also work with three-dimensional arrays.
data = np.array([
[
[1, 2],
[3, 4]
],
[
[5, 6],
[7, 8]
]
])
print(data.shape)
print(data.ndim)
(2, 2, 2) 3
The shape is:
(2, 2, 2)
You can think of this as:
2 blocks × 2 rows × 2 columns
Three-dimensional structures become important when working with things such as image data and neural networks.
Why Shape Matters in AI
Machine Learning and Deep Learning models expect data in specific shapes.
For example, imagine we have data for 100 students, and each student has 3 features:
Age Height Weight
The data could have a shape of:
(100, 3)
This means:
100 → students 3 → features for each student
So the array could look conceptually like:
[
[age, height, weight],
[age, height, weight],
[age, height, weight],
...
]
Understanding this becomes critical when preparing datasets for Machine Learning models.
Array Shape and Images
Images are another good example of why shape matters.
Suppose a grayscale image has:
28 rows 28 columns
Its shape can be:
(28, 28)
A color image usually has an additional dimension for the color channels.
For example:
(224, 224, 3)
This can represent:
224 → image height 224 → image width 3 → color channels
This is why understanding array shapes is essential for computer vision and Deep Learning.
Shape vs Size
Do not confuse shape with size.
data = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(data.shape)
print(data.size)
(2, 3) 6
The shape tells us the structure:
(2, 3) → 2 rows → 3 columns
The size tells us the total number of elements:
2 × 3 = 6
Complete Example
Let's put everything together:
import numpy as np
students = np.array([
[20, 170, 65],
[22, 175, 70],
[25, 180, 80],
[21, 168, 60]
])
print("Data:")
print(students)
print("Shape:", students.shape)
print("Dimensions:", students.ndim)
print("Total elements:", students.size)
Data: [[ 20 170 65] [ 22 175 70] [ 25 180 80] [ 21 168 60]] Shape: (4, 3) Dimensions: 2 Total elements: 12
Now we can clearly understand the result:
Shape (4, 3) 4 → students 3 → features Dimensions 2 → rows and columns Size 12 → 4 × 3
A Simple Rule to Remember
When you see a NumPy shape, read it from left to right.
(5,) → 5 elements (2, 3) → 2 rows × 3 columns (4, 5) → 4 rows × 5 columns (2, 3, 4) → 2 × 3 × 4
The number of values inside the shape tells you the number of dimensions.
(5,) → 1 dimension (2, 3) → 2 dimensions (2, 3, 4) → 3 dimensions
Shape tells you the structure of a NumPy array.
Use array.shape to see the size of the
array along each dimension, array.ndim
to see how many dimensions it has, and
array.size to see the total number of
elements. For example, (100, 3) means
100 rows and 3 columns. Understanding shapes is
critical because AI and Machine Learning models
expect data in specific shapes.