Linear Algebra
Matrix and Vector Products
| Routine | Synopsis |
|---|---|
np.dot(a, b[, out]) |
Dot product of two arrays |
np.linalg.multi_dot(arrays, *[, out]) |
Compute the dot product of two or more arrays in a single function call, while automatically selecting the fastest evaluation order |
np.vdot(a, b, /) |
Return the dot product of two vectors |
np.inner(a, b, /) |
Inner product of two arrays |
np.outer(a, b[, out]) |
Compute the outer product of two vectors |
np.matmul(x1, x2, /[, out, casting, order, ...]) |
Matrix product of two arrays |
np.tensordot(a, b[, axes]) |
Compute tensor dot product along specified axes |
np.einsum(subscripts, *operands[, out, dtype, ...]) |
Evaluates the Einstein summation convention on the operands |
np.einsum_path(subscripts, *operands[, optimize]) |
Evaluates the lowest cost contraction order for an einsum expression by considering the creation of intermediate arrays |
np.linalg.matrix_power(a, n) |
Raise a square matrix to the (integer) to the power n |
np.kron(a, b) |
Kronecker product of two arrays |
Note: the statements included in the ‘Routine’ column assume NumPy is loaded with the np alias.
Decompositions
| Routine | Synopsis |
|---|---|
np.linalg.cholesky(a) |
Cholesky decomposition |
np.linalg.qr(a[, mode]) |
Compute the qr factorization of a matrix |
np.linalg.svd(a[, full_matrices, compute_uv, ...]) |
Singular Value Decomposition |
Note: the statements included in the ‘Routine’ column assume NumPy is loaded with the np alias.
Matrix Eigenvalues
| Routine | Synopsis |
|---|---|
np.linalg.eig(a) |
Compute the eigenvalues and right eigenvectors of a square array |
np.linalg.eigh(a[, UPLO]) |
Return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix |
np.linalg.eigvals(a) |
Compute the eigenvalues of a general matrix |
np.linalg.eigvalsh(a[, UPLO]) |
Compute the eigenvalues of a complex Hermitian or real symmetric matrix |
Note: the statements included in the ‘Routine’ column assume NumPy is loaded with the np alias.
Norms and Other Numbers
| Routine | Synopsis |
|---|---|
np.linalg.norm(x[, ord, axis, keepdims]) |
Matrix or vector norm |
np.linalg.cond(x[, p]) |
Compute the condition number of a matrix |
np.linalg.det(a) |
Compute the determinant of an array |
np.linalg.matrix_rank(A[, tol, hermitian]) |
Return matrix rank of array using SVD method |
np.linalg.slogdet(a) |
Compute the sign and (natural) logarithm of the determinant of an array |
np.trace(a[, offset, axis1, axis2, dtype, out]) |
Return the sum along diagonals of the array |
Note: the statements included in the ‘Routine’ column assume NumPy is loaded with the np alias.
Solving Equations and Inverting Matrices
| Routine | Synopsis |
|---|---|
np.linalg.solve(a, b) |
Solve a linear matrix equation, or system of linear scalar equations |
np.linalg.tensorsolve(a, b[, axes]) |
Solve the tensor equation a x = b for x |
np.linalg.lstsq(a, b[, rcond]) |
Return the least-squares solution to a linear matrix equation |
np.linalg.inv(a) |
Compute the (multiplicative) inverse of a matrix |
np.linalg.pinv(a[, rcond, hermitian]) |
Compute the (Moore-Penrose) pseudo-inverse of a matrix |
np.linalg.tensorinv(a[, ind]) |
Compute the ‘inverse’ of an N-dimensional array |
Note: the statements included in the ‘Routine’ column assume NumPy is loaded with the np alias.
Matrix and Vector Products
# import numpy with the socially accepted alias 'np'
>>> import numpy as np
# the arrays
>>> X = np.arange(1, 11, 1)
>>> X
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> Y = np.arange(10, 0, -1)
>>> Y
array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
# dot product
>>> np.dot(X, Y)
220
# inner product
# --+ let's reshape the arrays
>>> X = X.reshape(5, 2)
>>> X
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10]])
>>> Y = Y.reshape(5, 2)
>>> Y
array([[10, 9],
[ 8, 7],
[ 6, 5],
[ 4, 3],
[ 2, 1]])
>>> np.inner(X, Y)
array([[ 28, 22, 16, 10, 4],
[ 66, 52, 38, 24, 10],
[104, 82, 60, 38, 16],
[142, 112, 82, 52, 22],
[180, 142, 104, 66, 28]])
# outer product
>>> np.outer([0, 1, 2], [4, 5, 6, 7])
array([[ 0, 0, 0, 0],
[ 4, 5, 6, 7],
[ 8, 10, 12, 14]])Solving a System of Equations
# import numpy with the socially accepted alias 'np'
>>> import numpy as np
# the system of equations
# --+ left-hand side
>>> a = np.array([[4, 2], [7, 13]])
# --+ right-hand side
>>> b = np.array([2000, 3000])
# solve the system of equations
>>> np.linalg.solve(a, b)
array([526.31578947, -52.63157895])Least-Square Estimation
# import numpy with the socially accepted alias 'np'
>>> import numpy as np
# the arrays
>>> x = np.arange(0, 15, 1)
>>> y = [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31]
# data preparation / adding a constant to the model
>>> A = np.vstack([x, np.ones(len(x))]).T
>>> A
array([[ 0., 1.],
[ 1., 1.],
[ 2., 1.],
[ 3., 1.],
[ 4., 1.],
[ 5., 1.],
[ 6., 1.],
[ 7., 1.],
[ 8., 1.],
[ 9., 1.],
[10., 1.],
[11., 1.],
[12., 1.],
[13., 1.],
[14., 1.]])
# estimate the regression coefficients (a.k.a., the regression slopes) of
# the linear model
>>> b = np.linalg.lstsq(A, y)[0]
>>> b
array([2., 3.])