Compute the Moore-Penrose pseudoinverse of a matrix in Python


To Compute the (Moore-Penrose) pseudo-inverse of a matrix, use the numpy.linalg.pinv() method in Python. Calculate the generalized inverse of a matrix using its singular-value decomposition (SVD) and including all large singular values.

The 1st parameter, a is a Matrix or stack of matrices to be pseudo-inverted. The 2nd parameter, rcodn is cutoff for small singular values. Singular values less than or equal to rcond * largest_singular_value is set to zero. Broadcasts against the stack of matrices. The 3rd parameter, hermitian, if True, a is assumed to be Hermitian, enabling a more efficient method for finding singular values. Defaults to False.

Steps

At first, import the required libraries-

import numpy as np

Create an array and fill with random values using randn() −

arr = np.random.randn(9, 6)

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

Get the Shape −

print("\nShape of our Array object...\n",arr.shape)

To compute the (Moore-Penrose) pseudo-inverse of a matrix, use the numpy.linalg.pinv() method −

print("\nResult...\n",np.linalg.pinv(arr))

Example

import numpy as np

# Create an array and fill with random values using randn()
arr = np.random.randn(9, 6)

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array object...\n",arr.shape)

# To Compute the (Moore-Penrose) pseudo-inverse of a matrix, use the numpy.linalg.pinv() method in Python.
print("\nResult...\n",np.linalg.pinv(arr))

Output

Our Array...
[[ 2.14644893 -0.14757929 0.14252834 0.54433625 -0.21374741 0.08804508]
[-0.05644831 -0.75323572 -1.95304923 0.17167461 -0.64155798 -1.38576017]
[-1.40043868 -0.62073383 -0.13501655 0.79788858 -1.47284176 1.03076414]
[ 0.52384943 -0.51581571 -0.35674166 1.32374059 -0.31340491 0.26292693]
[-0.28434997 0.07384262 1.62577397 -0.54059147 -1.02090985 2.36613533]
[-0.22025823 -1.07203572 1.30598633 0.39122889 2.05180917 1.59262088]
[-2.53455261 0.79274529 0.1822599 1.11345144 0.54343454 0.27523291]
[-1.11915817 1.21435385 0.87345865 0.85541497 1.90349169 -0.05778244]
[ 0.99636776 0.83682256 -0.03753307 -0.11389184 1.14089214 0.11317533]]

Dimensions of our Array...
2

Datatype of our Array object...
float64

Shape of our Array object...
(9, 6)

Result...
[[ 0.19229685 -0.03266066 -0.05913054 0.0990068 0.01377734 -0.02829296
-0.11340774 -0.02715551 0.13106032]
[ 0.01242764 -0.03612164 -0.0019295 0.00090135 0.15372234 -0.31686534
0.16305901 0.09059529 0.45836714]
[ 0.23344397 -0.46295399 -0.17382325 -0.0801975 -0.10227208 -0.04366331
-0.14434698 0.2615106 -0.84357154]
[ 0.28299012 -0.06772757 0.11355691 0.31272279 -0.11283442 -0.0361218
0.12165585 0.17999476 -0.14682526]
[-0.11148768 0.11063486 -0.07823299 -0.03096356 -0.07104466 0.24122668
0.02395283 0.01890529 0.26797921]
[-0.13235983 0.21188986 0.20340676 0.09081754 0.31058622 0.13372814
0.11417357 -0.20740154 0.71096452]]

Updated on: 25-Feb-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements