SciPy linalg.pinv() Function



The SciPy linalg.pinv() function is the Moore-Penrose pseudo inverse extends the concept of the matrix inverse to situations where the matrix might not be invertible. When the matrix A is invertible, the Moore-Penrose pseudo inverse is the same as the regular inverse is the same as the regular inverse. However, the Moore-Penrose pseudo inverse remains well-defined even if A is not invertible.

The scipy.linalg.pinv() function is utilized to calculate the Moore-Penrose pseudo-inverse of a matrix.

We compute the generalized inverse of a matrix by utilizing its singular value decomposition, represented as U@S@V. Using the economy mode, we select only the columns or rows corresponding to significant singular values.

If s is the maximum singular value of the matrix a, the atol+rtol*s is the significant cut-off value. Values below this cut-off are considered insignificant.

Syntax

Following is the syntax for the SciPy linalg.pinv() function.

scipy.linalg.pinv(a, atol=None, rtol=None, return_rank=False,check_finite=True, cond=None, rcond=None)

Parameters

The parameters for the scipy.linalg.pinv() function are listed below −

  • a: This parameter accepts an array representing a matrix that requires pseudo-inversion.

  • atol: This parameter accepts a float datatype that signifies the absolute threshold term. It is optional, with a default value of 0.

  • rtol: This parameter accepts a float data type that represents the relative threshold term. It is optional, with a default value of max(M,N)*eqs, where eps denotes the machine precision value for the data type of the input matrix a.

  • return_rank: This parameter accepts a Boolean data type. If set to True, it returns the effective rank of the matrix. It is an optional parameter.

  • check_finite: This parameter accepts a Boolean data type and is used to check whether the input matrix contains only finite numbers. Disabling this parameter improves performance; however, if the input matrix contains infinities or NaNs, it may lead to issues such as crashes or non-termination. The default value is True.

  • cond,rcond: In earlier versions, these values were meant to be used as atol with rtol=0. If both rcond and cond were specified, rcond would overwrite cond, leading to incorrect code. Therefore, it is strongly recommended to use the tolerance listed above instead of these.

Return Value

The linalg.pinv() function accepts the above parameters and returns the pseudo-inverse of the input matrix a in the form of an ndarray.

This function also returns the effective rank of the matrix when the return-rank parameter is specified and set to True.

Example 1

Here's an example code that demonstrates converting an input matrix a into its pseudo-inverse using the linalg.pinv() function. We utilize NumPy arrays to pass the input matrix. This code imports SciPy and NumPy, calculates the pseudo-inverse of matrix a with linalg.pin(), and prints the result.

from scipy import linalg
import numpy as np
a = np.array([[ 4,  5,  6,  7, 8, 9],
               [ 2,  2,  2,  2, 2, 0],
               [-1, -1, -1, -1, 0, 0]])
b = linalg.pinv(a)
print('The pseudo inverse matrix of the input matrix a is',b)

Output

The result is generated as follows −

The pseudo inverse matrix of the input matrix a is 
[[-1.74418605e-02  6.97674419e-02 -2.06395349e-01]
 [-5.81395349e-03  2.32558140e-02 -2.35465116e-01]
 [ 5.81395349e-03 -2.32558140e-02 -2.64534884e-01]
 [ 1.74418605e-02 -6.97674419e-02 -2.93604651e-01]
 [ 2.76884028e-18  5.00000000e-01  1.00000000e+00]
 [ 1.04651163e-01 -4.18604651e-01 -2.61627907e-01]]

Example 2

In this example code below, we use NumPy's random number generator to create an input matrix. The code generates a random matrix, calculates its pseudo-inverse, and verifies the accuracy using NumPy's np.allclose() function.

from scipy import linalg
import numpy as np
random_values = np.random.default_rng()
a = random_values.standard_normal((3, 6))
b = linalg.pinv(a)
print('The pseudo inverse matrix of the input matrix a is',b)
print(np.allclose(a, a @ b @ a))
print(np.allclose(b, b @ a @ b))

Output

The code is generated as follows −

The pseudo inverse matrix of the input matrix a is 
[[ 0.14830525  0.0357137   0.21186802]
 [ 0.07563623  0.21374762  0.13800089]
 [ 0.12940637  0.1264689  -0.02320093]
 [-0.11602253  0.03673745  0.23377344]
 [ 0.11465407 -0.35255313  0.29859209]
 [ 0.13446911  0.22479002 -0.19690538]]
True
True

Example 3

Here's an example code where we provide an input matrix and use the check_finite parameter. The check_finite parameter checks for any NaNs or infinities in the input matrices. If chek_finite is set to True, it returns an error if any NaNs or infinities are found.

from scipy import linalg
import numpy as np
a = np.array([[ 4,  np.nan,  6,  7, 8, 9],
               [ 2,  2,  2,  2, 2, 0],
               [-1, -1, -1, -1, np.nan, 0]])
b = linalg.pinv(a, check_finite=True)
print('The pseudo inverse matrix of the input matrix a is',b)

Output

The output is obtained as follows −

Traceback (most recent call last):
  File "/home/main.py", line 6, in <module>
    b = linalg.pinv(a, check_finite=True)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/scipy/linalg/_basic.py", line 1433, in pinv
    a = _asarray_validated(a, check_finite=check_finite)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/scipy/_lib/_util.py", line 240, in _asarray_validated
    a = toarray(a)
        ^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/numpy/lib/function_base.py", line 630, in asarray_chkfinite
    raise ValueError(
ValueError: array must not contain infs or NaNs
parent_file.htm
Advertisements