SciPy linalg.pinv2() Function



The scipy.linalg.pinv2() function is employed to compute the Moore-Penrose pseudo-inverse of a matrix.

The scipy.linalg.pinv() function is equivalent to scipy.linalg.pinv2(). However, scipy.linalg.pinv2() has been deprecated since SciPy 1.7.0. It is recommended to use scipy.linalg.pinv() for better tolerance control. In this case, we compute the generalized inverse of the matrix by utilizing its singular value decomposition and including all significant singular values.

The Moore-Penrose pseudo-inverse generalizes the concept of the matrix inverse for cases where the matrix may not be invertible. When A is invertible, the Moore-Penrose pseudo-inverse is identical to the regular matrix inverse. However, the Moorse-Penrose pseudo-inverse remains well-defined even if A is not invertible.

Syntax

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

scipy.linalg.pinv2(a, cond=None, rcond=None, return_rank=False, check_finite=True)

Parameters

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

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

  • cond, rcond: This parameter accepts a float data type and is used to determine the cutoff for small singular values. Singular values smaller than this threshold are considered zero. If both cond and rcond are specified, the default value used is max(M,N)*largest_singular_value *eps, where eps is 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 optional.

  • 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 performances, but if the input matrix contains infinities or NaNs, it may cause issues such as crashes or non-termination. The default value is True.

Return Value

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

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

Example 1

The linalg.pinv2() demonstrates how to convert an input matrix into its pseudo-inverse. This function computes the Moore-Penrose pseudo-inverse of a matrix, which is particularly useful when dealing with non-square matrices that may not be invertible.

This code takes an input matrix a and utilizes NumPy's linalg.pinv2() function to compute its pseudo-inverse. The matrix a is provided as NumPy array, allowing for efficient matrix operations. The pinv2() function handles both square and non-square matrices and provides a way to obtain a matrix inverse approximation in cases where the standard matrix inverse doesn't exist.

from scipy import linalg
import numpy as np
a = np.array([[ 4,  9,  6,  7, 8, 9],
               [ 2,  2,  2,  2, 2, 0],
               [-8, -1, -1, -1, 0, 0],
             [8, 9, 4, 5, 2, -8]])
b=linalg.pinv2(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 [[-0.00655242 -0.00280998 -0.13661794 -0.00899698]
 [ 0.11911962 -0.64101002  0.03364415  0.13321853]
 [-0.05611559  0.47251624  0.02998992 -0.0531194 ]
 [-0.01058468  0.19097362  0.02930948 -0.00812332]
 [-0.04586694  0.48033014  0.0436744  -0.06297883]
 [ 0.0813172  -0.24824709 -0.05453629 -0.03150762]]

Example 2

We utilize NumPy's random number generator to create an input matrix. After generating the matrix, the code computes its pseudo-inverse using the linalg.pinv2() function. To verify the accuracy of the result, the np.allclose() function is used, which checks the computed pseudo-inverse.

In this example, the code generates a random matrix using NumPy's random number generator. This matrix is then passed to the linalg.pinv2() function to compute its pseudo-inverse.

from scipy import linalg
import numpy as np
random_values = np.random.default_rng()
a = random_values.standard_normal((5, 6))
b = linalg.pinv2(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.15701096  1.99918121 -1.33253735 -1.53680301  0.3731628 ]
 [ 0.25731049 -1.16286552  0.80853     0.92064805  0.00433598]
 [ 0.32248723  0.12147455  0.73253818 -0.1412802  -0.58157834]
 [ 0.30821145  1.68283498 -2.98384394 -2.54884014  1.7614994 ]
 [-0.54914402 -0.58538222  1.72580277  1.60843496 -1.62681507]
 [ 0.65913107  0.45608604  0.325372   -1.12140879  0.91887492]]
True
True

Example 3

Here is an example code where we provide an input matrix and use the check_finite parameter with the linalg.pinv2() function. The check_finite parameter checks for any NaNs or infinities in the input matrices. When the cjeck_finite parameter 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,  9,  6,  7, 8, 9],
               [ 2,  2,  2,  np.nan, 2, 0],
               [-8, np.nan, -1, -1, 0, 0],
             [8, 9, 4, 5, 2, np.nan]])
b = linalg.pinv2(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/cg/root/26383/main.py", line 7, in 
    b = linalg.pinv2(a, check_finite=True)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/scipy/linalg/_basic.py", line 1433, in pinv2
    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