在Python中消除非正方形矩阵的线性相关列 [英] elimination the linear dependent columns of a non-square matrix in python

查看:59
本文介绍了在Python中消除非正方形矩阵的线性相关列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵A = np.array([[1,1,1],[1,2,3],[4,4,4]]),我只想在新数组中使用线性独立的行矩阵.答案可能是 A_new = np.array([1,1,1],[1,2,3]]) 或 A_new = np.array([1,2,3],[4,4,4])

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?

推荐答案

执行此操作的方法有很多,哪种方法最好取决于您的需求.而且,正如您在声明中指出的那样,甚至没有唯一的输出.

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.

一种执行此操作的方法是使用Gram-Schmidt查找正交基础,其中此基础上的前$ k $个向量与前$ k $个独立行的跨度相同.如果在任何步骤中发现线性相关性,请从矩阵中删除该行,然后继续执行该过程.

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.

使用numpy做到这一点的简单方法是

A simple way do do this with numpy would be,

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

,然后删除R_ {i,i}为零的所有列.

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

例如,您可以

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

虽然这在精确的算术中可以完美地工作,但在有限的精度下可能无法很好地工作.几乎所有矩阵在数值上都是独立的,因此您需要某种阈值确定是否存在线性相关性.如果使用内置QR方法,则必须确保不依赖于先前删除的列.

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]    

使用稳定的直接方法.如果您可以精确解决,则A.T [:,k]取决于先前的向量,并由x给出组合.

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.

这篇关于在Python中消除非正方形矩阵的线性相关列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆