The generalized inverse of matrix is often found in machine learning code. This paper first summarizes the generalized inverse of matrix conceptually, and then uses Python numpy library practice.

concept

The Generalized inverse of a matrix, also known as pseudo inverse, assumes a matrixAnd another matrixIf,meet,That’s the generalized inverse of A.

Put forward the reason of generalized inverse of matrix

The purpose of generalized inverse of matrices is to find a matrix that has some properties similar to inverse for matrices other than invertible matrices (such as non-square matrices). Any matrix has generalized inverse. If a matrix has an inverse matrix, the inverse matrix is its unique generalized inverse matrix.

Consider the following system of linear equations:

Where A isY is A member of the column space of A.

If A is invertible,Is the solution to the equation. And if A is invertible

If A is not invertible or , need amatrixMake the following formula valid:


soIs a system of linear equationsSolution of thisOrder of the matricesAlso make theWas established.

So the generalized inverse can be defined in the following way: if oneThe matrix A of,The matrix ofIf you can makeIf true, then the matrixIt’s the generalized inverse of A.

Kinds of generalized inverses of matrices

ifand

(1)

(2)

(3)

(4) .

ifSatisfies (1), which is the generalized inverse of A.

If (1) and (2) are satisfied, it is the generalized reflexive inverse of A.

If all four conditions are satisfied, it is Moore — Penrose Pseudoinverse of A.

Python practice

The generalized inverse of the matrix is obtained by numpy. Linalg. Pinv practice.

Define a matrix
In [26]: A=np.array([[1.2], [3.4], [5.6]])

In [27]: A
Out[27]: 
array([[1.2],
       [3.4],
       [5.6]])

Find the pseudo inverse of the matrix
In [28]: pinv = np.linalg.pinv(A)

In [29]: pinv
Out[29]: 
array([[1.33333333.0.33333333.0.66666667],
       [ 1.08333333.0.33333333.0.41666667]])
Copy the code

Test the

# Intermediate variable
In [31]: mid = np.dot(A, pinv)

It satisfies the definition of pseudo inverse and returns the original matrix A
In [33]: np.dot(mid,A)
Out[33]: 
array([[1..2.],
       [3..4.],
       [5..6.]])

Copy the code

So it can be seen that the result of numpy.linalg.pinv (the pinV matrix above) satisfies


References:

[1] Wikipedia generalized inverse matrix

[2] numpy.linalg.pinv