Time for action – computing the pseudo inverse of a matrix

Let's compute the pseudo inverse of a matrix. Perform the following steps to do so:

  1. First, create a matrix as follows:
    A = np.mat("4 11 14;8 7 -2")
    print "A
    ", A

    The matrix we created looks like the following:

    A
    [[ 4 11 14]
     [ 8  7 -2]]
    
  2. Calculate the pseudoinverse matrix with the pinv function, as follows:
    pseudoinv = np.linalg.pinv(A)
    print "Pseudo inverse
    ", pseudoinv

    The following is the pseudoinverse:

    Pseudo inverse
    [[-0.00555556  0.07222222]
     [ 0.02222222  0.04444444]
     [ 0.05555556 -0.05555556]]
    
  3. Multiply the original and pseudoinverse matrices.
    print "Check", A * pseudoinv

    What we get is not an identity matrix, but it comes close to it, as follows:

    Check [[  1.00000000e+00   0.00000000e+00]
     [  8.32667268e-17   1.00000000e+00]]
    

What just happened?

We computed the pseudoinverse of a matrix with the pinv function of the numpy.linalg module. The check by matrix multiplication resulted in a matrix that is approximately an identity matrix (see pseudoinversion.py).

import numpy as np

A = np.mat("4 11 14;8 7 -2")
print "A
", A

pseudoinv = np.linalg.pinv(A)
print "Pseudo inverse
", pseudoinv

print "Check", A * pseudoinv
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset