elimination the linear dependent columns of a non-square matrix in python

时光怂恿深爱的人放手 提交于 2020-12-13 03:44:52

问题


I have a matrix A = np.array([[1,1,1],[1,2,3],[4,4,4]]) and I want only the linearly independent rows in my new matrix. The answer might be A_new = np.array([1,1,1],[1,2,3]]) or A_new = np.array([1,2,3],[4,4,4])

Since I have a very large matrix so I need to decompose the matrix into smaller linearly independent full rank matrix. Can someone please help?


回答1:


There are many ways to do this, and which way is best will depend on your needs. And, as you noted in your statement, there isn't even a unique output.

One way to do this would be to use Gram-Schmidt to find an orthogonal basis, where the first $k$ vectors in this basis have the same span as the first $k$ independent rows. If at any step you find a linear dependence, drop that row from your matrix and continue the procedure.

A simple way do do this with numpy would be,

q,r = np.linalg.qr(A.T)

and then drop any columns where R_{i,i} is zero.

For instance, you could do

A[np.abs(np.diag(R))>=1e-10]

While this will work perfectly in exact arithmetic, it may not work as well in finite precision. Almost any matrix will be numerically independent, so you will need some kind of thresholding to determine if there is a linear dependence. If you use the built in QR method, you will have to make sure that there is no dependence on columns which you previously dropped.

If you need even more stability, you could iteratively solve the least squares problem

A.T[:,dependent_cols] x = A.T[:,col_to_check]    

using a stable direct method. If you can solve this exactly, then A.T[:,k] is dependent on the previous vectors, with the combination given by x.

Which solver to use may also be dictated by your data type.



来源:https://stackoverflow.com/questions/53667174/elimination-the-linear-dependent-columns-of-a-non-square-matrix-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!