Compute the Moore-Penrose pseudoinverse of a stack of matrices in Python


To Compute the (Moore-Penrose) pseudo-inverse of a stack of matrices, 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 using array().

arr = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]] ])

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 stack of matrices, use the numpy.linalg.pinv() method in Python.

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

Example

import numpy as np

# Create an array using array()
arr = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]] ])

# 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 stack of matrices, use the numpy.linalg.pinv() method in Python.
print("\nResult...\n",np.linalg.pinv(arr))

Output

Our Array...
[[[1 2]
[3 4]]

[[1 2]
[2 1]]

[[1 3]
[3 1]]]

Dimensions of our Array...
3

Datatype of our Array object...
int64

Shape of our Array object...
(3, 2, 2)

Result...
[[[-2. 1. ]
[ 1.5 -0.5 ]]

[[-0.33333333 0.66666667]
[ 0.66666667 -0.33333333]]

[[-0.125 0.375 ]
[ 0.375 -0.125 ]]]

Updated on: 25-Feb-2022

234 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements