Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
Linear algebra is a fundamental area of mathematics, and it's essential in many domains such as data science, machine learning, computer graphics, and more. NumPy provides a comprehensive suite of functions to work with linear algebra, housed in the numpy.linalg
module.
Let's delve into linear algebra with NumPy.
Linear algebra deals with vectors, vector spaces, linear transformations, and matrices. NumPy, thanks to its linalg
module, offers a collection of tools to perform these operations efficiently.
Start by importing NumPy:
import numpy as np
The dot product of two vectors:
a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) dot_product = np.dot(a, b) print(dot_product) # Outputs: 32
Using the dot
function or @
operator:
A = np.array([[1, 2], [3, 4]]) B = np.array([[2, 0], [1, 3]]) result = np.dot(A, B) # or result = A @ B
A_transposed = A.T
eigenvalues, eigenvectors = np.linalg.eig(A)
U, s, V = np.linalg.svd(A)
norm = np.linalg.norm(A)
inverse = np.linalg.inv(A)
det = np.linalg.det(A)
To solve the system of linear equations Ax = B
, you can use:
x = np.linalg.solve(A, B)
Used for decomposing a Hermitian, positive-definite matrix into the product of a lower triangular matrix and its conjugate transpose.
L = np.linalg.cholesky(A)
result = np.kron(A, B)
NumPy provides a powerful suite of linear algebra tools that allow efficient operations on matrices and vectors. This module is foundational for various algorithms and techniques in machine learning, physics simulations, and much more. It's beneficial to become familiar with these operations if you intend to delve deeper into numerical and scientific computations using Python.
Note: Always remember that not all matrices are invertible. Ensure to handle exceptions or use functions like np.linalg.pinv()
(pseudo-inverse) when dealing with matrices that might be singular.
Description: NumPy provides a comprehensive set of linear algebra operations for matrices and vectors, including matrix multiplication, eigenvalue decomposition, and more.
Code:
import numpy as np # Example of linear algebra operations matrix = np.array([[1, 2], [3, 4]]) vector = np.array([5, 6]) # Matrix-vector multiplication matrix_vector_product = np.dot(matrix, vector) # Eigenvalue decomposition eigenvalues, eigenvectors = np.linalg.eig(matrix) print("Matrix:") print(matrix) print("Vector:") print(vector) print("Matrix-Vector Product:") print(matrix_vector_product) print("Eigenvalues:") print(eigenvalues) print("Eigenvectors:") print(eigenvectors)
Description: NumPy's numpy.linalg
module provides various linear algebra functions for common operations like solving linear equations, calculating eigenvalues, and more.
Code:
import numpy as np # Example of NumPy linear algebra functions matrix = np.array([[1, 2], [3, 4]]) vector = np.array([5, 6]) # Solve linear equations Ax = b solution = np.linalg.solve(matrix, vector) print("Matrix:") print(matrix) print("Vector:") print(vector) print("Solution to Ax = b:") print(solution)
Description: NumPy's numpy.linalg.solve
function is used to solve systems of linear equations of the form Ax = b.
Code:
import numpy as np # Example of solving linear equations with NumPy matrix = np.array([[2, 1], [1, 3]]) vector = np.array([8, 9]) # Solve linear equations Ax = b solution = np.linalg.solve(matrix, vector) print("Matrix:") print(matrix) print("Vector:") print(vector) print("Solution to Ax = b:") print(solution)
Description: NumPy's numpy.linalg.eig
function is used to compute the eigenvalues and eigenvectors of a square matrix.
Code:
import numpy as np # Example of computing eigenvalues and eigenvectors with NumPy matrix = np.array([[2, -1], [1, 3]]) # Compute eigenvalues and eigenvectors eigenvalues, eigenvectors = np.linalg.eig(matrix) print("Matrix:") print(matrix) print("Eigenvalues:") print(eigenvalues) print("Eigenvectors:") print(eigenvectors)
Description: NumPy supports various matrix operations, including matrix multiplication, element-wise addition, and more.
Code:
import numpy as np # Example of NumPy matrix operations matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) # Matrix multiplication matrix_product = np.dot(matrix1, matrix2) # Element-wise addition elementwise_sum = matrix1 + matrix2 print("Matrix 1:") print(matrix1) print("Matrix 2:") print(matrix2) print("Matrix Product:") print(matrix_product) print("Element-wise Sum:") print(elementwise_sum)
Description: NumPy's numpy.linalg.det
function is used to calculate the determinant of a square matrix.
Code:
import numpy as np # Example of calculating determinant with NumPy matrix = np.array([[2, 1], [1, 3]]) # Calculate determinant determinant = np.linalg.det(matrix) print("Matrix:") print(matrix) print("Determinant:") print(determinant)
Description: NumPy's numpy.linalg.inv
function is used to compute the inverse of a square matrix.
Code:
import numpy as np # Example of matrix inversion with NumPy matrix = np.array([[2, 1], [1, 3]]) # Compute matrix inverse inverse_matrix = np.linalg.inv(matrix) print("Matrix:") print(matrix) print("Inverse Matrix:") print(inverse_matrix)
Description: NumPy's numpy.linalg.svd
function is used to perform singular value decomposition (SVD) on a matrix.
Code:
import numpy as np # Example of singular value decomposition with NumPy matrix = np.array([[1, 2], [3, 4]]) # Perform singular value decomposition u, s, vh = np.linalg.svd(matrix) print("Matrix:") print(matrix) print("U matrix:") print(u) print("S matrix (singular values):") print(np.diag(s)) print("Vh matrix:") print(vh)
Description: Combining various NumPy linear algebra functions to solve a system of linear equations, calculate eigenvalues, and perform other operations.
Code:
import numpy as np # Example of various NumPy linear algebra operations matrix_a = np.array([[2, 1], [1, 3]]) vector_b = np.array([8, 9]) # Solve linear equations Ax = b solution_x = np.linalg.solve(matrix_a, vector_b) # Compute eigenvalues and eigenvectors eigenvalues, eigenvectors = np.linalg.eig(matrix_a) print("Matrix A:") print(matrix_a) print("Vector B:") print(vector_b) print("Solution to Ax = b:") print(solution_x) print("Eigenvalues:") print(eigenvalues) print("Eigenvectors:") print(eigenvectors)