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:
First, create a matrix as follows:
A = np.mat("4 11 14;8 7 -2") print "A\n", A
The matrix we created looks like the following:
A [[ 4 11 14] [ 8 7 -2]]
Calculate the pseudoinverse matrix with the
pinv
function, as follows:pseudoinv = np.linalg.pinv(A) print "Pseudo inverse\n", pseudoinv
The following is the pseudoinverse:
Pseudo inverse [[-0.00555556 0.07222222] [ 0.02222222 0.04444444] [ 0.05555556 -0.05555556]]
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...