
- 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.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, inb = 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