Updated March 23, 2023
What is NumPy Linear Algebra
The following article provides an outline for NumPy Linear Algebra. Linear algebra is central to almost all areas of mathematics and computer science. The data is represented by linear equations such as (a_{1}x_{1} +……+a_{n}x_{n} = b), which are presented in the form of matrices and vectors.
Matrix and Vector Products
Here are some of the functions of matrix and vector products which are given below:
Function |
Description |
dot(a, b) | Computes the dot product of two arrays. |
vdot(a, b) | Computes the dot product of two vectors. |
linalg.multi_dot(a,b,c,d,…) | Computes the dot product of multiple arrays at once. |
inner(a, b) | Computes the inner product of two arrays. |
outer(a, b) | Computes the outer product of two arrays. |
matmul(x1, x2) | Computes the matrix product of two arrays. |
tensordot(a, b,axes) | Computes the tensor dot product of two arrays along the specified axes. |
linalg.matrix_power(a, n) | Raises a square matrix raised to the given power. |
kron(a, b) | Computes the Kronecker product of two arrays. |
einsum(subscripts,*operand) | Evaluates the Einstein summation convention on the operands. |
Example #1
Calculating different type of products of given arrays:
Code:
import numpy as np
#creating two arrays a and b
a = np.array([2, 1, 2])
b= np.array([4,5,6])
#dot product
print("Dot Product of a and b:", np.dot(a,b))
#inner product
print("Inner Product of a and b:", np.inner(a,b))
#outer product
print("Outer Product of a and b:", np.outer(a,b))
#Kronecker product
print("Kronecker Product of a and b:", np.kron(a,b))
Output:
Let’s see how we calculate all the mentioned products:
Dot product is a.b = a_{i}*b_{i}+a_{j}*b_{j}+a_{k}*b_{k}= [2,1,2].[4,5,6] = 2*4+1*5+2*6 = 25
Inner product is a generalization of dot product. So, it is also calculated similarly to a dot product.
Outer Product of two matrices a and b of sizes (m x 1) and (n x 1) is a resultant matrix (m x n). The product is given by a ⛒ b.
a ⛒ b = [2*4 2*5 2*6] [1*4 1*5 1*6][2*4 2*5 2*6] =[ 8 10 12] [ 4 5 6 ] [ 8 10 12]
Kronecker product is the generalization of the outer product. It is also given by a ⛒ b [k_{0},k_{1},…,k_{N}] = a[i_{0},i_{1},…,i_{N}] * b[j_{0},j_{1},…,j_{N}] . The result is given in the form of a block matrix.a ⛒ b = [2*4 2*5 2*6] [1*4 1*5 1*6][2*4 2*5 2*6] = [8 10 12] [4 5 6] [8 10 12]
a ⛒ b = [2 1 2][4 5 6] = [8 10 12 4 5 6 8 10 12]
Example #2
This is the example of computing the matrix multiplication.
Code:
#creating matrix A and matrix B
import numpy as np
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
B = np.array([[2,1,3],[4,1,1],[1,2,3]])
#matrix multiplication
print("Multiplication of A and B:", np.matmul(A,B))
#raise matrix A to power of 2, i.e, AXA
print("Matrix A raised to power of 2:", np.linalg.matrix_power(A,2))
Output:
Matrix Eigenvalues
Here are some of the functions of matrix eigenvalues which are given below:
Function | Description |
linalg.eig(a) | Computes the eigenvalue of a square matrix. |
linalg.eigvals(a) | Compute the eigenvalues of any matrix. |
linalg.eigh(a) | Computes the eigenvalues and eigenvectors of a complex Hermitian and a real symmetric matrix. |
linalg.eigvalsh(a) | Compute the eigenvalues of a complex Hermitian and real symmetric matrix. |
Example #1
Computing Eigen Values of the given matrix.
Code:
import numpy as np
C = np.array([[1,0,0],[0,1,0],[0,0,1]])
print("Array is:",C)
#eigen values
print("Eigen values of Array is:",np.linalg.eig(C))
print("Eigen values of Array is:",np.linalg.eigvals(C))
Output:
To find eigenvalues, we find the values of λ that satisfy the matrix C’s characteristic equation, where det(C − λI) = 0, here I is a 3×3 identity matrix.
[1 0 0] [λ 0 0] (1-λ 0 0) [0 1 0] – [0 λ 0] = (0 1-λ 0) [0 0 1] [0 0 λ] (0 0 1-λ)Solving Equations and Inverting MatricesDet(A- λI) = 3-3λ=> 3 – 3λ = 0 => λ = 1
Hence, [1 1 1] is the eigenvalues.
Here are some of the functions of matrix and vector products which are given below:
Function | Description |
linalg.solve(a, b) | Finds the solution of a linear equation. |
linalg.tensorsolve(a, b) | Finds the solution of a tensor equation. |
linalg.pinv(a) | Computes the pseudo inverse of an array/matrix. |
linalg.tensorinv(a)
linalg.inv(a) |
Computes the inverse of an array/matrix. |
Example #2
Solve two linear equations using the matrix.
Code:
#Equation 1 : 3x+4y = 7
#Equation 2 : 4x+3y = 7
#creating two arrays, one for solution and one for equations
import numpy as np
A = np.array([[3,4],[4,3]])
B = np.array([7,7])
C = np.linalg.solve(A,B)
# solution of equations
print("Solution of given linear equations is:",C)
Output:
Example #3
This is an example of finding the inverse of a matrix.
Code:
import numpy as np
#creating a matrix A
A = np.array([[1,1],[0,1]])
#inverse of A
print("Inverse of matrix A is :", np.linalg.inv(A))
Output:
Miscellaneous
Here are some of the functions of miscellaneous which are given below:
Function | Description |
linalg.det(a) | Computes determinant of an array. |
linalg.slogdet(a) | Computes sign and the natural log of the determinant of an array. |
trace(a) | Computes the sum of diagonal elements of a square matrix. |
linalg.matrix_rank(a) | Computes matrix rank of a given array. |
linalg.norm(a) | Computes the norm of a matrix or vector. |
Example
Computing determinant, rank, and sum of diagonals of a matrix.
Code:
import numpy as np
C = np.array([[1,0,0],[0,1,0],[0,0,1]])
print("Array is:", C)
#determinant
print("Determinant of Array is:", np.linalg.det(C))
#sum of diagonal elements
print("Trace of Array is:", np.trace(C))
#rank of matrix C
print("Rank of matrix C is:", np.linalg.matrix_rank(C))
Output:
Conclusion
This post saw some of the most important numpy linear equation functions. One of the most important applications of these functions is machine learning, where we provide input to machine models in the form of matrices, vectors, and tensors. So, to be a successful Machine Learning Engineer or Data Scientist, one should be comfortable with these functions.
Recommended Articles
This is a guide to NumPy Linear Algebra. Here we discuss the different functions of NumPy linear algebra along with their examples and code implementation. You may also look at the following articles to learn more –