Linear Algebra Operations in NumPy
NumPy provides a powerful module called numpy.linalg
for linear algebra operations, which are essential in machine learning, physics, and engineering.
8. Matrix Creation
# Creating two matrices
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print("Matrix A:\n", A)
print("Matrix B:\n", B)
Matrix Multiplication (Dot Product)
# Matrix multiplication
dot_product = np.dot(A, B)
print("Dot Product:\n", dot_product)
Alternative:
# You can also use the @ operator for matrix multiplication.
print("Dot Product using @ operator:\n", A @ B)
Matrix Transpose
# Swaps rows and columns
print("Transpose of A:\n", A.T)
Determinant of a Matrix
# Computing determinant
print("Determinant of A:", np.linalg.det(A))
Inverse of a Matrix
# Computing inverse
print("Inverse of A:\n", np.linalg.inv(A))
Eigenvalues and Eigenvectors
# Computing eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
9. Why Linear Algebra is Important?
- Used in machine learning models, especially Principal Component Analysis (PCA).
- Essential for neural networks and deep learning.
- Helps in solving systems of linear equations.