3.3 Numerical Linear Algebra


: 20 minutes

NumPy offers functions for matrix operations such as addition, multiplication, dot product, decompositions, etc. These operations also apply to higher-dimensional arrays.

Basic Operations

The dot product two vectors can be computed using numpy.dot().

Recall that * computes the element-wise product of two arrays. To perform matrix multiplication, one can either use @ or numpy.dot.

numpy.linalg

Matrix decompositions such as LU, SVD, and operations like inverse and determinant are offered through the numpy.linalg module.

Commonly used numpy.linalg functions (McKinney 2017)
Method Description
diag Return the diagonal (or off-diagonal) elements of a square matrix as a 1D array, or convert a 1D array into a square matrix with zeros on the off-diagonal
dot Matrix multiplication
trace Compute the sum of the diagonal elements
det Compute the matrix determinant
eig Compute the eigenvalues and eigenvectors of a square matrix
inv Compute the inverse of a square matrix
pinv Compute the Moore-Penrose pseudoinverse of a matrix
qr Compute the QR decomposition
svd Compute the singular value decomposition (SVD)
solve Solve the linear system Ax = b for x, where A is a square matrix
McKinney, Wes. 2017. Python for Data Analysis. 2nd ed. O’Reilly Media. https://www.oreilly.com/library/view/python-for-data/9781491957653/.