MATHEMATICAL OPERATIONS IN NUMPY
Linear Algebra Operations
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)
| |
| |
| dot_product = np.dot(A, B) |
| print("Dot Product:\n", dot_product) |
Alternative:
| |
| |
| print("Dot Product using @ operator:\n", A @ B) |
Matrix Transpose
| |
| |
| print("Transpose of A:\n", A.T) |
Determinant of a Matrix
| |
| |
| print("Determinant of A:", np.linalg.det(A)) |
Inverse of a Matrix
| |
| |
| print("Inverse of A:\n", np.linalg.inv(A)) |
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.