Numpy: Transpose
Transposing matrices is a fundamental operation in linear algebra. It swaps the rows and columns of a matrix. In Numpy, transposing matrices is particularly straightforward and can be done using the T attribute or the transpose() function.
Bernd Fischer
11. Oct 2024 · 3 min read
Installing Numpy
If numpy is not yet installed, it can be installed via pip:
pip install numpy
Creating a Matrix with numpy.arange and Transposing
First, a matrix is created with the function arange, which generates a series of numbers and is shaped into the desired form with reshape.
Example 1: Transposing a Simple 2x3 Matrix
import numpy as np
# Eine 2x3-Matrix erstellen
matrix1 = np.arange(1, 7).reshape(2, 3)
# Visualisierung der Matrix
print("Originale Matrix 1 (2x3):\n", matrix1)
# Transponieren der Matrix
transposed_matrix1 = matrix1.T
# Visualisierung der transponierten Matrix
print("Transponierte Matrix 1 (3x2):\n", transposed_matrix1)
Output:
Originale Matrix 1 (2x3):
[[1 2 3]
[4 5 6]]
Transponierte Matrix 1 (3x2):
[[1 4]
[2 5]
[3 6]]
In this example, a 2x3 matrix is created. After transposing, the rows and columns are swapped, resulting in a 3x2 matrix.
Example 2: Transposing a 3x3 Matrix
# Eine 3x3-Matrix erstellen
matrix2 = np.arange(1, 10).reshape(3, 3)
# Visualisierung der Matrix
print("Originale Matrix 2 (3x3):\n", matrix2)
# Transponieren der Matrix
transposed_matrix2 = matrix2.T
# Visualisierung der transponierten Matrix
print("Transponierte Matrix 2 (3x3):\n", transposed_matrix2)
Output:
Originale Matrix 2 (3x3):
[[1 2 3]
[4 5 6]
[7 8 9]]
Transponierte Matrix 2 (3x3):
[[1 4 7]
[2 5 8]
[3 6 9]]
This example involves a square 3x3 matrix. After transposing, the matrix remains square, but the elements are swapped between rows and columns.
Transposing Multidimensional Arrays
Transposing is not limited to two-dimensional matrices. Multidimensional arrays can also be transposed.
Example 3: Transposing a 2x3x2 Multidimensional Matrix
# Ein 3-dimensionales Array erstellen
matrix3d = np.arange(1, 13).reshape(2, 3, 2)
# Visualisierung des Arrays
print("Originales 3D-Array (2x3x2):\n", matrix3d)
# Transponieren des 3D-Arrays
transposed_matrix3d = np.transpose(matrix3d, axes=(1, 0, 2))
# Visualisierung der transponierten Matrix
print("Transponiertes 3D-Array (3x2x2):\n", transposed_matrix3d)
Output:
Originales 3D-Array (2x3x2):
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
Transponiertes 3D-Array (3x2x2):
[[[ 1 2]
[ 7 8]]
[[ 3 4]
[ 9 10]]
[[ 5 6]
[11 12]]]
In this example, a three-dimensional array is created and transposed. By specifying the order of axes in np.transpose, the order of the dimensions changes. The result is a rearrangement of dimensions from (2, 3, 2) to (3, 2, 2).
Transposing Vectors
Vectors (1D arrays) can also be transposed. However, this often does not produce visible changes since vectors do not have distinct rows and columns.
Example 4: Transposing a Vector
# Einen Vektor (1D-Array) erstellen
vector = np.arange(1, 5)
# Visualisierung des Vektors
print("Originaler Vektor:\n", vector)
# Transponieren des Vektors (1D bleibt unverändert)
transposed_vector = vector.T
# Visualisierung des transponierten Vektors
print("Transponierter Vektor:\n", transposed_vector)
Output:
Originaler Vektor:
[1 2 3 4]
Transponierter Vektor:
[1 2 3 4]
Transposing a one-dimensional array (vector) does not alter its structure because there is no second dimension to swap.