获取超定线性齐次系统numpy的解 [英] Get solution to overdetermined linear homogeneous system numpy

查看:94
本文介绍了获取超定线性齐次系统numpy的解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用numpy找到超定线性齐次系统(Ax = 0)的解决方案,以获得线性回归的最小线性平方解决方案.

I'm trying to find the solution to overdetermined linear homogeneous system (Ax = 0) using numpy in order to get the least linear squares solution for a linear regression.

这是我用来生成线性回归的代码:

This is the code I am using to generate the linear regression:

N = 100
x_data = np.linspace(0, N-1, N)
m = +5
n = -5
y_model = m*x_data + n
y_noise = y_model + np.random.normal(0, +5, N)

我想从 y_noise 中恢复 m n .换句话说,我要解析齐次系统(Ax = 0),其中" x =(m,n) "和" A =(x_data | 1 | -y_noise) ".因此,我使用以下代码将非齐次(Ax = y)转换为齐次(Ax = 0):

I want to recover m and n from y_noise. In other words, I want to resolve the homogeneous system (Ax = 0) where "x = (m, n)" and "A = (x_data | 1 | -y_noise)". So I convert non-homogeneous (Ax = y) into homogeneous (Ax = 0) using this code:

A = np.array(np.vstack((x_data, np.ones(N), -y_noise)).T)

我知道我可以使用 np.linalg.lstsq((x_data | 1),y_noise)) 解决非均匀系统,但是我想获得解决方案均质系统.我发现此函数存在问题,因为它仅返回平凡的解决方案(x = 0):

I know I could resolve non-homogeneous system using np.linalg.lstsq((x_data | 1), y_noise)) but I want to get the solution for homogeneous system. I am finding a problem with this function as it only returns the trivial solution (x = 0):

x = np.linalg.lstsq(A, np.zeros(N))[0] => array([ 0.,  0.,  0.])

我当时正在考虑使用特征向量来获取解决方案,但似乎不起作用:

I was thinking about using eigenvectors to get the solution but it seems not to work:

A_T_A = np.dot(A.T, A)
eigen_values, eigen_vectors = np.linalg.eig(A_T_A)
# eigenvectors
[[ -2.03500000e-01   4.89890000e+00   5.31170000e+00]
 [ -3.10000000e-03   1.02230000e+00  -2.64330000e+01]
 [  1.00000000e+00   1.00000000e+00   1.00000000e+00]]
# eigenvectors normalized
[[ -0.98365497700  -4.744666220   1.0]  # (m1, n1, 1)
 [  0.00304878118   0.210130914   1.0]  # (m2, n2, 1)
 [  25.7752417000  -5.132910010   1.0]] # (m3, n3, 1)

它们都不符合模型参数(m = + 5,n = -5)

Which none of them fits model parameters (m=+5, n=-5)

如何正确找到(m,n)?谢谢!

How can I find (m, n) correctly? Thanks!

推荐答案

我已经找到了解决方法,问题是我如何解释 np.linalg.eig 函数的输出,但是使用特征向量的方法是正确的.尽管如此,当 @Stelios 表示函数 np.linalg.lstsq 返回平凡解(x = 0)时,它在右边,因为矩阵A已满列排名.

I have already found how to fix it, the problem is how I was interpreting the output of np.linalg.eig function, but the approach using eigenvectors is right. In spite of that, @Stelios is in the right when he says that the function np.linalg.lstsq returns the trivial solution (x = 0) because matrix A is full column rank.

我假设 np.linalg.eig 的输出是:

[[m1 n1  1]
 [m2 n2  1]
 [m3 n3  1]]

但不是,正确的格式是:

But it is not, the correct format is:

[[m1 m2 m3]
 [n1 n2 n3]
 [ 1  1  1]]

因此,如果要获得更适合模型参数(m,n)的解决方案,则必须选择特征值最小的特征向量并将其归一化:

So if we want to get the solution which better fits model paramaters (m, n), we have to choose the eigenvector with the smallest eigenvalue and normalize it:

A_T_A = np.dot(A_homo.T, A_homo)
eigen_values, eigen_vectors = np.linalg.eig(A_T_A)  
# eigenvectors
[[  1.96409304e-01   9.48763118e-01  -2.47531678e-01]
 [  2.94608003e-04   2.52391765e-01   9.67625088e-01]
 [ -9.80521952e-01   1.90123494e-01  -4.92925776e-02]]
# MIN eigenvector
eigen_vector_min = eigen_vectors[:, np.argmin(eigen_values)]
[-0.24753168  0.96762509 -0.04929258]
# MIN eigenvector normalized
[  5.02168258 -19.63023915   1.        ]   # [m, n, 1]

最后我们得到 m = 5.02 n = -19,6 .

这篇关于获取超定线性齐次系统numpy的解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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