Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Matrix and linear Algebra calculations in Python
In this article, we will learn Matrix and linear Algebra calculations in Python such as matrix multiplication, finding determinants, solving linear equations, etc.
A matrix object from the NumPy library can be used for this. When it comes to calculation, matrices are relatively comparable to the array objects. Linear Algebra is a huge topic, however, NumPy is an excellent library to start if you need to manipulate matrices and vectors.
Operations Covered
Finding Transpose of a Matrix Using Numpy
Finding Inverse of a Matrix Using Numpy
Multiplying Matrix with a Vector
Getting the Determinant of Matrix using numpy.linalg subpackage
Finding Eigenvalues using numpy.linalg
Solving equations using numpy.linalg
Finding Transpose of a Matrix
The numpy.matrix.T attribute returns the transpose of the given matrix ?
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5], [2, 0, 8], [1, 4, 3]])
print("Input Matrix:\n", inputMatrix)
# printing the transpose of an input matrix
print("Transpose of an input matrix:\n", inputMatrix.T)
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Transpose of an input matrix: [[6 2 1] [1 0 4] [5 8 3]]
Finding Inverse of a Matrix
The numpy.matrix.I attribute returns the inverse of the given matrix ?
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
print("Input Matrix:\n", inputMatrix)
# printing the inverse of an input matrix
print("Inverse of an input matrix:\n", inputMatrix.I)
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Inverse of an input matrix: [[ 0.21333333 -0.11333333 -0.05333333] [-0.01333333 -0.08666667 0.25333333] [-0.05333333 0.15333333 0.01333333]]
Multiplying Matrix with a Vector
Matrix-vector multiplication uses the * operator with NumPy matrices ?
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
print("Input Matrix:\n", inputMatrix)
# creating a vector using numpy.matrix() function
inputVector = np.matrix([[1],[3],[5]])
# printing the multiplication of the input matrix and vector
print("Multiplication of input matrix and vector:\n", inputMatrix*inputVector)
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Multiplication of input matrix and vector: [[34] [42] [28]]
Getting the Determinant of Matrix
The numpy.linalg.det() function calculates the determinant of a square matrix ?
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
print("Input Matrix:\n", inputMatrix)
# getting the determinant of an input matrix
outputDet = np.linalg.det(inputMatrix)
# printing the determinant of an input matrix
print("Determinant of an input matrix:\n", outputDet)
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Determinant of an input matrix: -149.99999999999997
Finding Eigenvalues
The numpy.linalg.eigvals() function calculates the eigenvalues of a specified square matrix ?
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
print("Input Matrix:\n", inputMatrix)
# getting Eigenvalues of an input matrix
eigenValues = np.linalg.eigvals(inputMatrix)
# printing Eigenvalues of an input matrix
print("Eigenvalues of an input matrix:\n", eigenValues)
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Eigenvalues of an input matrix: [ 9.55480959 3.69447805 -4.24928765]
Solving Linear Equations
We can solve linear equation systems like A × X = B, where A is the matrix and B is the vector, using numpy.linalg.solve() ?
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
print("Input Matrix:\n", inputMatrix)
# creating a vector using np.matrix() function
inputVector = np.matrix([[1],[3],[5]])
# getting the value of x in equation: inputMatrix * x = inputVector
x_value = np.linalg.solve(inputMatrix, inputVector)
print("x value:\n", x_value)
# verification: multiplying input matrix with x values
print("Verification (A × X):\n", inputMatrix * x_value)
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] x value: [[-0.39333333] [ 0.99333333] [ 0.47333333]] Verification (A × X): [[1.] [3.] [5.]]
Conclusion
NumPy provides comprehensive tools for matrix and linear algebra operations including transpose, inverse, determinant, eigenvalues, and solving linear equation systems. These functions form the foundation for more advanced numerical computing and data science applications.
