Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Matrixoperationen in Python | Grundlagen der Linearen Algebra
Mathematik für Data Science

bookMatrixoperationen in Python

1. Addition und Subtraktion

Zwei Matrizen AA und BB mit gleicher Form können addiert werden:

123456789
import numpy as np A = np.array([[1, 2], [5, 6]]) B = np.array([[3, 4], [7, 8]]) C = A + B print(f'C:\n{C}') # C = [[4, 6], [12, 14]]
copy

2. Multiplikationsregeln

Matrizenmultiplikation ist nicht elementweise.

Regel: Wenn AA die Form (n,m)(n, m) und BB die Form (m,l)(m, l) hat, dann hat das Ergebnis die Form (n,l)(n, l).

1234567891011121314151617181920
import numpy as np # Example random matrix 3x2 A = np.array([[1, 2], [3, 4], [5, 6]]) print(f'A:\n{A}') # Example random matrix 2x4 B = np.array([[11, 12, 13, 14], [15, 16, 17, 18]]) print(f'B:\n{B}') # product shape (3, 4) product = np.dot(A, B) print(f'np.dot(A, B):\n{product}') # or equivalently product = A @ B print(f'A @ B:\n{product}')
copy

3. Transponieren

Transponieren vertauscht Zeilen und Spalten.

Allgemeine Regel: Ist AA (n×m)(n \times m), dann ist ATA^T (m×n)(m \times n).

1234567
import numpy as np A = np.array([[1, 2, 3], [4, 5, 6]]) A_T = A.T # Transpose of A print(f'A_T:\n{A_T}')
copy

4. Inverse einer Matrix

Eine Matrix AA besitzt eine Inverse A1A^{-1}, wenn gilt:

AA1=IA \cdot A^{-1} = I

Dabei ist II die Einheitsmatrix.

Nicht alle Matrizen besitzen eine Inverse. Eine Matrix muss quadratisch und vollen Rangs sein.

12345678910
import numpy as np A = np.array([[1, 2], [3, 4]]) A_inv = np.linalg.inv(A) # Inverse of A print(f'A_inv:\n{A_inv}') I = np.eye(2) # Identity matrix 2x2 print(f'A x A_inv = I:\n{np.allclose(A @ A_inv, I)}') # Check if product equals identity
copy
question mark

Was ist die Ausgabe dieses Python-Codes?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 4

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain why matrix multiplication is not element-wise?

How do I know if a matrix is full-rank?

What happens if I try to invert a non-square matrix?

bookMatrixoperationen in Python

Swipe um das Menü anzuzeigen

1. Addition und Subtraktion

Zwei Matrizen AA und BB mit gleicher Form können addiert werden:

123456789
import numpy as np A = np.array([[1, 2], [5, 6]]) B = np.array([[3, 4], [7, 8]]) C = A + B print(f'C:\n{C}') # C = [[4, 6], [12, 14]]
copy

2. Multiplikationsregeln

Matrizenmultiplikation ist nicht elementweise.

Regel: Wenn AA die Form (n,m)(n, m) und BB die Form (m,l)(m, l) hat, dann hat das Ergebnis die Form (n,l)(n, l).

1234567891011121314151617181920
import numpy as np # Example random matrix 3x2 A = np.array([[1, 2], [3, 4], [5, 6]]) print(f'A:\n{A}') # Example random matrix 2x4 B = np.array([[11, 12, 13, 14], [15, 16, 17, 18]]) print(f'B:\n{B}') # product shape (3, 4) product = np.dot(A, B) print(f'np.dot(A, B):\n{product}') # or equivalently product = A @ B print(f'A @ B:\n{product}')
copy

3. Transponieren

Transponieren vertauscht Zeilen und Spalten.

Allgemeine Regel: Ist AA (n×m)(n \times m), dann ist ATA^T (m×n)(m \times n).

1234567
import numpy as np A = np.array([[1, 2, 3], [4, 5, 6]]) A_T = A.T # Transpose of A print(f'A_T:\n{A_T}')
copy

4. Inverse einer Matrix

Eine Matrix AA besitzt eine Inverse A1A^{-1}, wenn gilt:

AA1=IA \cdot A^{-1} = I

Dabei ist II die Einheitsmatrix.

Nicht alle Matrizen besitzen eine Inverse. Eine Matrix muss quadratisch und vollen Rangs sein.

12345678910
import numpy as np A = np.array([[1, 2], [3, 4]]) A_inv = np.linalg.inv(A) # Inverse of A print(f'A_inv:\n{A_inv}') I = np.eye(2) # Identity matrix 2x2 print(f'A x A_inv = I:\n{np.allclose(A @ A_inv, I)}') # Check if product equals identity
copy
question mark

Was ist die Ausgabe dieses Python-Codes?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 4
some-alt