In this tutorial, we’ll explore advanced linear algebra operations using NumPy. We will cover eigenvalues and eigenvectors analysis, singular value decomposition (SVD), and computation of vector and matrix norms. These techniques are essential for understanding matrix transformations and for various applications in data science and engineering.
1. Eigenvalues and Eigenvectors
Using np.linalg.eig()
, you can compute the eigenvalues and eigenvectors of a square matrix. This analysis is fundamental in various fields such as stability analysis, principal component analysis (PCA), and systems dynamics.
Code Example:
import numpy as np
# Define a 2x2 matrix
demo_matrix = np.array([
[1, 2],
[3, 4]
])
# Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(demo_matrix)
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:", eigenvectors)
Explanation of parameters:
np.linalg.eig()
accepts a square matrix and returns a tuple containing an array of eigenvalues and a matrix with the corresponding eigenvectors as columns.- Eigenvalues indicate the factors by which the eigenvectors are scaled during the transformation represented by the matrix.
2. Singular Value Decomposition (SVD)
SVD is a powerful technique for matrix factorization. The function np.linalg.svd()
decomposes any matrix into three components: U, Σ (singular values), and VT. This decomposition is widely used in signal processing, statistics, and data compression.
Code Example:
import numpy as np
# Define a 3x2 matrix
demo_matrix = np.array([
[1, 2],
[3, 4],
[5, 6]
])
# Perform Singular Value Decomposition
U, s, Vt = np.linalg.svd(demo_matrix)
print("U matrix:", U)
print("Singular values:", s)
print("V^T matrix:", Vt)
Explanation of parameters:
np.linalg.svd()
decomposes a matrix into three parts:U
contains the left singular vectors.s
is a 1D array of singular values, representing the magnitude of the principal components.VT
(V transpose) contains the right singular vectors.
3. Norms
Norms are measures that quantify the size or length of vectors and matrices. The function np.linalg.norm()
allows you to compute various types of norms, with the L2 norm (Euclidean norm) being the default.
Code Example:
import numpy as np
# Define a vector and a matrix
vector = np.array([3, 4])
demo_matrix = np.array([
[1, 2],
[3, 4]
])
# Compute the Euclidean norm for the vector and the matrix
vector_norm = np.linalg.norm(vector)
matrix_norm = np.linalg.norm(demo_matrix)
print("Vector Norm:", vector_norm)
print("Matrix Norm:", matrix_norm)
Explanation of parameters:
np.linalg.norm()
computes the magnitude (or length) of a vector or matrix. By default, it calculates the L2 norm (Euclidean norm).- You can specify different norm orders by providing the
ord
parameter if needed.
Summary
- Eigenvalues & Eigenvectors: Analyzed using
np.linalg.eig()
to determine the scaling factors and directions of matrix transformations. - Singular Value Decomposition (SVD): Decomposes a matrix into U, singular values, and VT using
np.linalg.svd()
, which is useful for dimensionality reduction and data analysis. - Norms: Computes the magnitude of vectors and matrices using
np.linalg.norm()
, with the L2 norm as the default measurement.