Зміст курсу
Mathematics for Data Analysis and Modeling
Mathematics for Data Analysis and Modeling
Inversed and Transposed Matrices
Matrix Transposition
In linear algebra, the transpose of a matrix is an operation that flips the matrix over its diagonal, resulting in a new matrix where the rows become columns, and the columns become rows. The transpose of a matrix A
is denoted as A^T
.
We can provide transponation of matrix in Python in two different ways:
- Using
.T
attribute ofnp.array
class; - Using
np.transpose()
method.
import numpy as np # Define a matrix matrix = np.array([[1, 2, 3], [4, 5, 6]]) # Calculate the transpose using the `.T` attribute transpose_matrix = matrix.T print('Original matrix:') print(matrix) print('\nTransposed matrix using .T attribute:') print(transpose_matrix) # Calculate the transpose using the `transpose()` function transpose_matrix = np.transpose(matrix) print('\nTransposed matrix using np.transpose():') print(transpose_matrix)
Matrix Inversion
In linear algebra, the inverse of a square matrix A is a matrix denoted as A^-1
, which, when multiplied by the original matrix A, results in the identity matrix I. The inverse of a matrix exists only if the determinant of the matrix is non-zero.
Note
An identity matrix, denoted as
I
, is a square matrix with ones on the main diagonal (from the top left to the bottom right) and zeros elsewhere. In other words, all elements in the main diagonal are equal to 1, while all other elements are equal to 0.
It's important to admit that A * A^-1 = A^-1 * A = I
- the multiplication operation by the inversed matrix is commutative.
In Python, you can use the .inv()
method of the np.linalg
module to calculate the inverse of a matrix. Here's an example:
import numpy as np # Define a matrix matrix = np.array([[1, 2], [3, 4]]) # Calculate the inverse using the `inv()` function inverse_matrix = np.linalg.inv(matrix) print('Original matrix:') print(matrix) print('\nInverse matrix:') print(inverse_matrix) # Multiply the original matrix by its inverse identity_matrix = np.dot(matrix, inverse_matrix) print('\nIdentity matrix:') print(np.round(identity_matrix, 3))
Дякуємо за ваш відгук!