Numpy linalg.pinv(): Computing the Pseudo-Inverse of a Matrix

Pseudo Inverse Of A Matrix

Most of us would be familiar with the term inverse while operating with matrices. But what on earth is a pseudo-inverse? If it is pseudo which in turn means a false entity, then why bother using it? This seemingly contradicting function is what we would be exploring in this article. The linalg.pinv( ) function is within the numpy library of Python, so let us get started by importing this library using the below code.

import numpy as np

Thereafter, we shall explore further the aforementioned function through each of the following sections.

  • Why and when to use a pseudo-inverse of a matrix?
  • Syntax of the linalg.pinv( ) function
  • Use cases for the linalg.pinv( ) function

Why and when to use a pseudo-inverse of a matrix?

It is time that we face the questions posed in the introduction of this article, heads on! The pseudo-inverse also known as the Moore-Penrose inverse named after those who introduced it finds its application in linear algebra. The pre-requisites for any matrix to be inverted are,

  • It should be a non-singular matrix
  • It should be a square matrix

All those that do not satisfy both of the above conditions cannot be inverted. But in most cases, while trying to solve a linear system of equations, the coefficient matrix will not be in line with the first condition. They find it hard to be a square matrix.

So, it makes it difficult to solve and find a solution directly. Thusly, a technique known as Singular Value Decomposition (SVD) is being used to deduce the best available solution by approximation and the inverse of the matrix generated by this process is called the pseudo-inverse.

Syntax of the linalg.pinv( ) function

Given below is the syntax of the linalg.pinv( ) function detailing the fundamental constituents required for its proper functioning.

numpy.linalg.pinv(a, rcond=1e-15, hermitian=False)


  • a – A matrix or a stack of matrices that are to be pseudo-inverted
  • rcond – Threshold for small singular values set to ‘1e-15’ by default. Those below the product of rcond and the largest singular value will be set to zero
  • hermitian – Set to ‘False’ by default and is used to declare whether ‘a’ contains symmetric real-valued matri(x)ces

Use cases for the linalg.pinv( ) function

To demonstrate the working of the linalg.pinv( ) function, let us create a couple of matrices with dimensions 3×4 and 4×1 respectively. Since the demonstration is to be done with the matrices, let us use the matrix( ) function from the numpy library for this purpose.

A = np.matrix([[-1, 2, 3,7],
               [12, 31, 5, 9],
               [1, -9, 4, 17]])
B = np.matrix([[2],

Now let us find the product of the above two matrices using the following code for matrix multiplication.

AB = [email protected]
Result Of Matrix Multiplication
Result Of Matrix Multiplication

Once done, let us move on with finding the pseudo-inverse of the resultant matrix given above using the linalg.pinv( ) function as shown below.

I = np.linalg.pinv(AB)
Pseudo Inverse Matrix Calculated
Pseudo Inverse Matrix Calculated

A comparison of the resultant matrix before and after being pseudo-inverted would give a clear idea of its functioning. The conventional technique of inversion does not allow this resultant matrix to be inverted since it is not square in dimensions. But, the linalg.pinv( ) gives a near approximation using the SVD.

At times, there might be an error code – LinAlgError – that pops up. This happens when the results of the SVD do not converge.


Now that we have reached the end of this article, hope it has elaborated on how to use the linalg.pinv( ) function from the numpy library. Here’s another article that details the usage of the linalg.tensorinv ( ) function from the numpy library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Carpe diem!