Statistics and maths / Linear algebra and matrices
Matrix Operations in Python
Introduction to Matrices in Python
Matrices are two-dimensional arrays that represent a collection of numbers arranged in rows and columns. Python provides several ways to work with matrices, including the NumPy library, which offers powerful tools for scientific computing and linear algebra operations.
import numpy as np
# Creating a matrix
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix)
Creating Matrices with NumPy
NumPy is a powerful library for working with arrays and matrices in Python. It provides various functions to create and manipulate matrices.
import numpy as np
# Creating a 2x3 matrix
matrix_1 = np.array([[1, 2, 3], [4, 5, 6]])
# Creating a 3x3 matrix with all zeros
matrix_2 = np.zeros((3, 3))
# Creating a 3x3 identity matrix
matrix_3 = np.eye(3)
Matrix Operations
NumPy supports various arithmetic operations on matrices, such as addition, subtraction, multiplication, and scalar operations.
import numpy as np
matrix_1 = np.array([[1, 2], [3, 4]])
matrix_2 = np.array([[5, 6], [7, 8]])
# Matrix addition
result_1 = matrix_1 + matrix_2
print(result_1)
# Matrix multiplication
result_2 = matrix_1 @ matrix_2
print(result_2)
# Scalar multiplication
result_3 = 2 * matrix_1
print(result_3)
Accessing Matrix Elements
Matrices can be indexed and sliced like regular NumPy arrays to access or modify their elements.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Accessing an element
print(matrix[1, 1]) # Output: 5
# Modifying an element
matrix[0, 0] = 10
print(matrix)
# Slicing a matrix
submatrix = matrix[0:2, 1:3]
print(submatrix)
Matrix Reshaping
NumPy provides functions to reshape matrices by changing their dimensions while preserving the order of elements.
import numpy as np
matrix = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(matrix)
# Reshaping a matrix
reshaped_matrix = matrix.reshape(4, 2)
print(reshaped_matrix)
# Flattening a matrix
flattened_matrix = matrix.flatten()
print(flattened_matrix)
Matrix Transposition
Transposing a matrix means interchanging its rows and columns. NumPy provides a convenient method for transposing matrices.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print("Original Matrix:")
print(matrix)
# Transposing a matrix
transposed_matrix = matrix.T
print("Transposed Matrix:")
print(transposed_matrix)
Matrix Multiplication
NumPy provides efficient matrix multiplication operations, which are essential for various mathematical and scientific applications.
import numpy as np
matrix_1 = np.array([[1, 2], [3, 4]])
matrix_2 = np.array([[5, 6], [7, 8]])
# Matrix multiplication
result = matrix_1 @ matrix_2
print(result)
Matrix Inverse and Determinant
NumPy offers functions to calculate the inverse and determinant of a square matrix, which are important concepts in linear algebra.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
# Calculating the inverse of a matrix
inverse_matrix = np.linalg.inv(matrix)
print(inverse_matrix)
# Calculating the determinant of a matrix
determinant = np.linalg.det(matrix)
print(determinant)
Eigenvalues and Eigenvectors
Eigenvalues and eigenvectors are fundamental concepts in linear algebra and have numerous applications in various fields, such as physics, engineering, and data analysis.
import numpy as np
matrix = np.array([[3, 1], [1, 3]])
# Calculating eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(matrix)
print("Eigenvalues:")
print(eigenvalues)
print("Eigenvectors:")
print(eigenvectors)
Matrix Decompositions
NumPy provides functions for matrix decompositions, such as LU decomposition, QR decomposition, and Singular Value Decomposition (SVD), which are useful in various applications.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# LU decomposition
P, L, U = np.linalg.lu(matrix)
print("P:\n", P)
print("L:\n", L)
print("U:\n", U)
Matrix Norms
Matrix norms are scalar values that measure the magnitude or size of a matrix. NumPy provides functions to calculate different types of norms, such as the Frobenius norm and the induced norms.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
# Frobenius norm
frobenius_norm = np.linalg.norm(matrix, 'fro')
print(frobenius_norm)
# Induced 2-norm (maximum singular value)
induced_norm = np.linalg.norm(matrix, 2)
print(induced_norm)
Solving Linear Systems
NumPy provides functions to solve linear systems of equations represented by matrices, which is a fundamental task in many scientific and engineering applications.
import numpy as np
A = np.array([[1, 2], [3, 4]])
b = np.array([5, 11])
# Solving the linear system Ax = b
x = np.linalg.solve(A, b)
print(x)
Broadcasting in Matrix Operations
NumPy supports broadcasting, which allows arithmetic operations between arrays with different shapes, following specific rules. This feature is useful for performing operations on matrices with scalars or vectors.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
scalar = 2
vector = np.array([1, 2, 3])
# Scalar multiplication
result_1 = matrix * scalar
print(result_1)
# Vector addition
result_2 = matrix + vector
print(result_2)
Slide 14 Additional Resources
For further learning and exploration of matrices in Python and NumPy, you can refer to the following resources:
- NumPy User Guide: https://numpy.org/doc/stable/user/index.html
- NumPy Reference: https://numpy.org/doc/stable/reference/index.html
- "Introduction to Linear Algebra" by G. Strang (book)
- ArXiv link: https://arxiv.org/abs/1711.06752 (Efficient NumPy Operations for Machine Learning)
Note: The ArXiv link provided is a research paper on efficient NumPy operations for machine learning, which may contain relevant information and examples related to matrix operations in NumPy.