Understanding Axis in NumPy
The axis parameter in NumPy is key to understanding how operations are performed on arrays β whether you're summing values, computing means, or concatenating arrays.
It helps you control the direction along which a function operates, especially in multi-dimensional arrays. Whether you're working with rows or columns, axis defines the behavior.
- In 2D Array:
- Axis 0: Operates down the rows (i.e., column-wise).
- Axis 1: Operates across the columns (i.e., row-wise).
# Visual layout of 3D Array (shape = (2, 2, 3)) Block 0: [[ 1, 2, 3], [ 4, 5, 6]] Block 1: [[ 7, 8, 9], [10, 11, 12]] - Axis 0: Moves between Block 0 and Block 1 - Axis 1: Moves between rows within a block - Axis 2: Moves between columns within a row
π‘ Tip: If no axis is specified, NumPy applies the operation to the entire array by default.
What Does Axis 0 Mean?
In a 3D NumPy array, axis 0 refers to movement between blocks β like moving from one 2D matrix to the next in a stack of tables. This is the βdepthβ axis.
When you perform an operation along axis=0, NumPy will take corresponding elements across all blocks and apply the operation. Itβs like collapsing the stack vertically.
Example: Summing Along Axis 0
import numpy as np
arr = np.array([
[[1, 2, 3],
[4, 5, 6]],
[[7, 8, 9],
[10, 11, 12]]
])
# arr.shape = (2, 2, 3)
# Sum along axis 0 (collapse blocks)
print(np.sum(arr, axis=0))
import numpy as np
arr = np.array([
[[1, 2, 3],
[4, 5, 6]],
[[7, 8, 9],
[10, 11, 12]]
])
# arr.shape = (2, 2, 3)
# Sum along axis 0 (collapse blocks)
print(np.sum(arr, axis=0))
How It Works
NumPy lines up the blocks and sums elements at the same positions:
- [1+7, 2+8, 3+9] β [8, 10, 12] β row 0
- [4+10, 5+11, 6+12] β [14, 16, 18] β row 1
The result is a single 2D array (same shape as each block): (2, 3)
[[ 8 10 12]
[14 16 18]]
[[ 8 10 12]
[14 16 18]]
π‘ You can think of axis 0 as collapsing the cube downward β combining values at the same positions across all layers.
What Does Axis 1 Mean?
In a 3D NumPy array, axis 1 moves down the rows within each block. That means it operates across rows in each individual 2D matrix (block).
When you perform an operation along axis=1, NumPy combines rows that are in the same block β working block by block, row-wise.
Example: Summing Along Axis 1
import numpy as np
arr = np.array([
[[1, 2, 3],
[4, 5, 6]],
[[7, 8, 9],
[10, 11, 12]]
])
# arr.shape = (2, 2, 3)
# Sum along axis 1 (collapse rows within each block)
print(np.sum(arr, axis=1))
import numpy as np
arr = np.array([
[[1, 2, 3],
[4, 5, 6]],
[[7, 8, 9],
[10, 11, 12]]
])
# arr.shape = (2, 2, 3)
# Sum along axis 1 (collapse rows within each block)
print(np.sum(arr, axis=1))
How It Works
NumPy goes inside each block and sums corresponding columns across rows:
- Block 0: [1+4, 2+5, 3+6] β [5, 7, 9]
- Block 1: [7+10, 8+11, 9+12] β [17, 19, 21]
The result is a 2D array with shape (2, 3), one row per block.
[[ 5 7 9]
[17 19 21]]
[[ 5 7 9]
[17 19 21]]
π‘ You can think of axis 1 as collapsing the rows within each block β combining them across columns.
What Does Axis 2 Mean?
Think of a 3D NumPy array as a collection of tables stacked on top of each other. Each βtableβ is a 2D matrix. So, a 3D array is like a cube of numbers.
Axis 0: Moves between tables (the "depth").
Axis 1: Moves down the rows in each table.
Axis 2: Moves across the columns of each row in each table.
So when you do an operation along axis 2, you're going across the columns of every row in every table β in other words, you're working with the individual rows.
Example: Summing Along Axis 2
import numpy as np
arr = np.array([[
[1, 2, 3],
[4, 5, 6]],
[
[ 7, 8, 9],
[10, 11, 12]
]])
# arr.shape = (2, 2, 3)
# Now apply np.sum(arr, axis=2) Youβre summing each row β so:
print(np.sum(arr, axis=2))
import numpy as np
arr = np.array([[
[1, 2, 3],
[4, 5, 6]],
[
[ 7, 8, 9],
[10, 11, 12]
]])
# arr.shape = (2, 2, 3)
# Now apply np.sum(arr, axis=2) Youβre summing each row β so:
print(np.sum(arr, axis=2))
How It Works
- Block 0 row 0 β 1+2+3 = 6
- Block 0 row 1 β 4+5+6 = 15
- Block 1 row 0 β 7+8+9 = 24
- Block 1 row 1 β 10+11+12 = 33
The result becomes a 2D array of shape (2, 2):
[[ 6 15]
[24 33]]
[[ 6 15]
[24 33]]
π‘ You can think of axis 2 as zooming in and operating across individual rows of each block.
Understanding Axis in a 4D NumPy Array
A 4D NumPy array adds another layer of structure β it's like a stack of 3D cubes. The shape of a 4D array is usually described as (a, b, c, d), where:
- Axis 0: Moves between 3D blocks (outermost layer)
- Axis 1: Moves between 2D blocks inside each 3D block
- Axis 2: Moves between rows inside 2D blocks
- Axis 3: Moves across columns inside a row
Letβs work with a 4D array of shape (2, 2, 2, 3). Here's how it looks and behaves when summing along different axes.
Example:
import numpy as np
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]]
]
])
# arr.shape = (2, 2, 2, 3)
# Sum along axis 0 β combine the two outer 3D blocks
print('np.sum(arr, axis = 0):')
print(np.sum(arr, axis=0))
print('np.sum(arr, axis = 1):')
print(np.sum(arr, axis=1))
print('np.sum(arr, axis = 2):')
print(np.sum(arr, axis=2))
print('np.sum(arr, axis = 3):')
print(np.sum(arr, axis=3))
import numpy as np
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]]
]
])
# arr.shape = (2, 2, 2, 3)
# Sum along axis 0 β combine the two outer 3D blocks
print('np.sum(arr, axis = 0):')
print(np.sum(arr, axis=0))
print('np.sum(arr, axis = 1):')
print(np.sum(arr, axis=1))
print('np.sum(arr, axis = 2):')
print(np.sum(arr, axis=2))
print('np.sum(arr, axis = 3):')
print(np.sum(arr, axis=3))
Output
np.sum(arr, axis = 0):
[[[14 16 18]
[20 22 24]]
[[26 28 30]
[32 34 36]]]
np.sum(arr, axis = 1):
[[[ 8 10 12]
[14 16 18]]
[[32 34 36]
[38 40 42]]]
np.sum(arr, axis = 2):
[[[ 5 7 9]
[17 19 21]]
[[29 31 33]
[41 43 45]]]
np.sum(arr, axis = 3):
[[[ 6 15]
[24 33]]
[[42 51]
[60 69]]]
np.sum(arr, axis = 0):
[[[14 16 18]
[20 22 24]]
[[26 28 30]
[32 34 36]]]
np.sum(arr, axis = 1):
[[[ 8 10 12]
[14 16 18]]
[[32 34 36]
[38 40 42]]]
np.sum(arr, axis = 2):
[[[ 5 7 9]
[17 19 21]]
[[29 31 33]
[41 43 45]]]
np.sum(arr, axis = 3):
[[[ 6 15]
[24 33]]
[[42 51]
[60 69]]]
How It Works
- Axis 0 β Combine outer 3D blocks:
- [1+13, 2+14, 3+15] β [14, 16, 18]
- [4+16, 5+17, 6+18] β [20, 22, 24]
- [7+19, 8+20, 9+21] β [26, 28, 30]
- [10+22, 11+23, 12+24] β [32, 34, 36]
- Axis 1 β Combine 2D blocks inside each 3D block:
- Block 0:
- [1+7, 2+8, 3+9] β [8, 10, 12]
- [4+10, 5+11, 6+12] β [14, 16, 18]
- Block 1:
- [13+19, 14+20, 15+21] β [32, 34, 36]
- [16+22, 17+23, 18+24] β [38, 40, 42]
- Axis 2 β Collapse rows in each 2D block:
- Block 0, Sub-block 0: [1+4, 2+5, 3+6] β [5, 7, 9]
- Block 0, Sub-block 1: [7+10, 8+11, 9+12] β [17, 19, 21]
- Block 1, Sub-block 0: [13+16, 14+17, 15+18] β [29, 31, 33]
- Block 1, Sub-block 1: [19+22, 20+23, 21+24] β [38, 40, 42]
- Axis 3 β Collapse columns (sum across each row):
- Block 0, Sub-block 0 row 0: 1+2+3 = 6
- Block 0, Sub-block 0 row 1: 4+5+6 = 15
- Block 0, Sub-block 1 row 0: 7+8+9 = 24
- Block 0, Sub-block 1 row 1: 10+11+12 = 33
- Block 1, Sub-block 0 row 0: 13+14+15 = 42
- Block 1, Sub-block 0 row 1: 16+17+18 = 51
- Block 1, Sub-block 1 row 0: 19+20+21 = 60
- Block 1, Sub-block 1 row 1: 22+23+24 = 69
Each axis peels away one layer and combines the values across that axis. Just like nesting loops: the deeper the axis number, the more internal the structure.
Frequently Asked Questions
What does axis mean in NumPy?
What does axis mean in NumPy?
In NumPy, an axis refers to a specific dimension along which operations like sum, mean, or concatenation are performed.
How does the axis parameter affect NumPy functions?
How does the axis parameter affect NumPy functions?
The axis parameter specifies the dimension along which the operation is applied. For example, summing along axis=0 sums columns, while axis=1 sums rows in a 2D array.
What is the difference between axis 0 and axis 1?
What is the difference between axis 0 and axis 1?
For 2D arrays, axis=0 refers to the vertical direction (down rows), and axis=1 refers to the horizontal direction (across columns).
Can axis values be negative?
Can axis values be negative?
Yes, negative axis values count dimensions from the last. For example, axis=-1 refers to the last axis.
Why is understanding axis important in NumPy?
Why is understanding axis important in NumPy?
Understanding axis is essential to correctly perform operations on multi-dimensional arrays, making sure calculations are done along the intended dimensions.
What's Next?
Next, weβll move on to Transposing NumPy Arrays β a technique used to flip the axes of an array, such as converting rows to columns or reversing dimensions.