
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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