• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:13 mins read
You are currently viewing NumPy Inverse Matrix in Python

NumPy linalg.inv() function in Python is used to compute the (multiplicative) inverse of a matrix. The inverse of a matrix is that matrix which when multiplied with the original matrix, results in an identity matrix. In this article, I will explain how to use the NumPy inverse matrix to compute the inverse of the matrix array using this function.

1. Quick Examples of Inverse Matrix

If you are in a hurry, below are some quick examples of how to use Python NumPy inverse matrix.


# Quick examples of inverse matrix

import numpy as np

# Example 1:Use numpy.linalg.inv() 
# Calculate the inverse of the matrix
inverse_matrix = np.linalg.inv(arr)

# Example 2: Using scipy.linalg.inv()
# Calculate the inverse of the matrix 
inverse_matrix = inv(arr)

# Example 3: Use np.linalg.inv() function 
arr = np.array([[[2., 6.], [5., 8.]], 
                [[3, 7], [4, 1]]])
arr2 = np.linalg.inv(arr)

2. Syntax of numpy.linalg.inv() Function

Following is the syntax to create numpy.linalg.inv() function.


# Syntax of numpy.linalg.inv() function
numpy.linalg.inv(arr)

2.1 Parameter of Inverse Matrix

Following are the parameters of the inverse matrix.

arr : This parameter represents the matrix to be inverted. This is the input matrix for which you want to compute the inverse. The input should be a square matrix (having the same number of rows and columns).

2.2 Return Value of Inverse Matrix

This function returns the inverse of the input matrix array. If the input matrix is singular or not square, it will raise a LinAlgError.

3. Usage of numpy.linalg.inv() Function

Using Python numpy.linalg.inv() function to the inverse of a matrix in simple mathematics can be defined as a matrix.

3.1 Use numpy.linalg.inv() Function

We can use a matrix as a rectangular arrangement of data or numbers, in other words, we can say that it is a rectangular array of data the horizontal entries in the matrix are called rows and the vertical entries are called columns. For the matrix inverse function, we need to use np.linalg.inv() function. This function will inverse the given matrix. Python NumPy provides an easy function to calculate the inverse of the matrix. The function helps the user to check numpy.linalg.inv() is available in the Python library.

In the below example, the np.linalg.inv() function is used to calculate the inverse of the 2×2 matrix. You can replace the matrix variable with any square matrix you want to find the inverse for.


# Import numpy
import numpy as np

# Creating an input array  
arr = np.array([[7, 2,], [3, -5]])
print("Original Matrix:\n",arr)
               
# Use numpy.linalg.inv() 
# Calculate the inverse of the matrix
inverse_matrix = np.linalg.inv(arr)
print("After getting the inverse of a matrix:\n",inverse_matrix)

Yields below output.

numpy inverse matrix

Keep in mind that not all matrices have inverses. Inverse exists only for square matrices that are full rank (i.e., their rows and columns are linearly independent). If you try to find the inverse of a non-invertible matrix, NumPy will raise a LinAlgError.

4. Get the Inverse of a Matrix Using scipy.linalg.inv() Function

We can also use the scipy module to perform different scientific calculations using its functionalities. Using scipy.linalg.inv() function is used to return the inverse of a given square matrix in NumPy Python. It works the same way as the numpy.linalg.inv() function.


import numpy as np
from scipy.linalg import inv

# Creating an input array  
arr = np.array([[7, 2,], [3, -5]])
print("Original Matrix:\n",arr)

# Using scipy.linalg.inv()
# Calculate the inverse of the matrix 
inverse_matrix = inv(arr)
print("After getting the inverse of a matrix:\n",inverse_matrix)

Yields the same output as above.

5. Inverse of a Matrix NumPy Two Multi-Dimensional Arrays

We can also use np.linalg.inv() function to compute the multiplicative inverse of a matrix of the two multi-dimensional arrays elementwise.


import numpy as np

# Inverse of 4X4 matrix   
arr = np.array([[[2., 6.], [5., 8.]],
               [[3, 7], [4, 1]]])
print("Original Matrix:\n",arr)

# Use np.linalg.inv() function 
inverse_matrix = np.linalg.inv(arr)
print("After getting the inverse of a matrix:\n",inverse_matrix)

# Output:
# After getting the inverse of a matrix:
#  [[[-0.57142857  0.42857143]
#  [ 0.35714286 -0.14285714]]

# [[-0.04        0.28      ]
#  [ 0.16       -0.12      ]]]

Frequently Asked Questions

What is the purpose of finding the inverse of a matrix?

Finding the inverse of a matrix is essential in various mathematical and engineering applications. It allows solving systems of linear equations, computing determinants, and performing transformations in computer graphics and machine learning

How do I calculate the inverse of a matrix using NumPy?

You can use the numpy.linalg.inv() function in NumPy to calculate the inverse of a matrix.

Can I find the inverse of any matrix?

Not every matrix has an inverse. For a matrix to have an inverse, it must be square (having the same number of rows and columns) and non-singular (its determinant should not be zero).

What happens if I try to find the inverse of a singular matrix using NumPy?

If you try to find the inverse of a singular matrix (a matrix with determinant zero), NumPy’s np.linalg.inv() function will raise a LinAlgError because singular matrices do not have inverses.

What are some common applications of matrix inversion in Python?

Matrix inversion is used in various fields like computer graphics (for transformations), solving systems of linear equations, optimization problems, and machine learning algorithms like linear regression.

Are there alternative methods for solving linear systems in NumPy?

NumPy provides several alternative methods for solving linear systems besides finding the inverse. One commonly used method is the numpy.linalg.solve() function. This function is more numerically stable and efficient than explicitly finding the inverse, especially for large systems of equations.

Conclusion

In this article, I have explained how to use the inverse matrix to compute the inverse of the matrix array with examples.

Happy Learning!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.