Numpy Axis Explained

In Numpy, axes play a central role in array manipulation. The concept of axes describes the dimensions along which operations like summing, averaging, or sorting are applied.

Bernd Fischer

Bernd Fischer

12. Oct 2024 · 5 min read

Numpy Axis Explained

1. What are Axes in Numpy?

In Numpy, axes are nothing other than the dimensions of an array. A one-dimensional array has only one axis, a two-dimensional array (e.g., a matrix) has two axes: rows and columns. Each axis is represented by a number (0, 1, 2, ...).

  • Axis 0: Refers to the rows in an array.
  • Axis 1: Refers to the columns in an array.

2. Axes in a One-Dimensional Array (1D Array)

A one-dimensional array (1D array) has only one axis. The only existing axis is axis 0.

Example: A One-Dimensional Array and Its Axis

import numpy as np

# Ein einfaches eindimensionales Array erstellen
array_1d = np.array([1, 2, 3, 4, 5])

# Visualisierung des Arrays
print("Eindimensionales Array:\n", array_1d)

Output:

Eindimensionales Array:
 [1 2 3 4 5]

In this case, there is only one axis (axis 0) which encompasses all elements of the array.

3. Axes in a Two-Dimensional Array (2D Array)

A two-dimensional array has two axes: axis 0 and axis 1. Axis 0 is the row axis and axis 1 is the column axis.

Example: A 2D Array and Its Axes

# Ein zweidimensionales Array erstellen
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Visualisierung des Arrays
print("Zweidimensionales Array (Matrix):\n", array_2d)

Output:

Zweidimensionales Array (Matrix):
 [[1 2 3]
  [4 5 6]
  [7 8 9]]

Here, the array has two axes:

  • Axis 0 (row axis): 1, 4, 7; 2, 5, 8; 3, 6, 9.
  • Axis 1 (column axis): 1, 2, 3; 4, 5, 6; 7, 8, 9.

4. Axes in a Three-Dimensional Array (3D Array)

A three-dimensional array has three axes: axis 0, axis 1, and axis 2. These correspond to depth, rows, and columns.

Example: A 3D Array and Its Axes

# Ein dreidimensionales Array erstellen
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])

# Visualisierung des Arrays
print("Dreidimensionales Array:\n", array_3d)

Output:

Dreidimensionales Array:
 [[[ 1  2]
   [ 3  4]]

  [[ 5  6]
   [ 7  8]]

  [[ 9 10]
   [11 12]]]

There are three axes:

  • Axis 0: Refers to the depth (the "layers" of the array).
  • Axis 1: Refers to the rows within each "layer".
  • Axis 2: Refers to the columns within each "layer".

5. Use of Axes in Operations

Axes in Numpy arrays are especially important when performing operations like summing, averaging, or finding the maximum along a specific dimension. Numpy operations such as sum, mean, or max can be controlled with the parameter axis to specify along which axis the calculation should occur.

Example: Summing Along Rows and Columns in a 2D Array

# Summe entlang der Zeilen (Achse 0)
summe_achse_0 = np.sum(array_2d, axis=0)

# Summe entlang der Spalten (Achse 1)
summe_achse_1 = np.sum(array_2d, axis=1)

# Visualisierung der Ergebnisse
print("Summe entlang der Zeilen (Achse 0):", summe_achse_0)
print("Summe entlang der Spalten (Achse 1):", summe_achse_1)

Output:

Summe entlang der Zeilen (Achse 0): [12 15 18]
Summe entlang der Spalten (Achse 1): [ 6 15 24]
  • The sum along the row axis (axis 0) sums the values in each column.
  • The sum along the column axis (axis 1) sums the values in each row.

Example: Maximum Along Axes in a 3D Array

# Maximum entlang der Achse 0 (Tiefenachse)
max_achse_0 = np.max(array_3d, axis=0)

# Maximum entlang der Achse 1 (Zeilenachse)
max_achse_1 = np.max(array_3d, axis=1)

# Maximum entlang der Achse 2 (Spaltenachse)
max_achse_2 = np.max(array_3d, axis=2)

# Visualisierung der Ergebnisse
print("Maximum entlang der Achse 0 (Tiefenachse):\n", max_achse_0)
print("Maximum entlang der Achse 1 (Zeilenachse):\n", max_achse_1)
print("Maximum entlang der Achse 2 (Spaltenachse):\n", max_achse_2)

Output:

Maximum entlang der Achse 0 (Tiefenachse):
 [[ 9 10]
  [11 12]]
Maximum entlang der Achse 1 (Zeilenachse):
 [[ 3  4]
  [ 7  8]
  [11 12]]
Maximum entlang der Achse 2 (Spaltenachse):
 [[ 2  4]
  [ 6  8]
  [10 12]]
  • The maximum along axis 0 returns the maximum of each position along the depth.
  • The maximum along axis 1 returns the maximum of each row in each layer.
  • The maximum along axis 2 returns the maximum of each column in each layer.