如何在权重矩阵中使用最小二乘法? [英] How to use least squares with weight matrix?
本文介绍了如何在权重矩阵中使用最小二乘法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何使用Python用最小二乘法求解A.X = B:
I know how to solve A.X = B by least squares using Python:
示例:
A=[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,0,0]]
B=[1,1,1,1,1]
X=numpy.linalg.lstsq(A, B)
print X[0]
# [ 5.00000000e-01 5.00000000e-01 -1.66533454e-16 -1.11022302e-16]
但是在权重矩阵不是恒等的情况下解决这个相同的方程呢?
But what about solving this same equation with a weight matrix not being Identity:
A.X = B (W)
示例:
A=[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,0,0]]
B=[1,1,1,1,1]
W=[1,2,3,4,5]
推荐答案
我发现了另一种方法(使用W作为对角矩阵和矩阵乘积):
I found another approach (using W as a diagonal matrix, and matricial products) :
A=[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,0,0]]
B = [1,1,1,1,1]
W = [1,2,3,4,5]
W = np.sqrt(np.diag(W))
Aw = np.dot(W,A)
Bw = np.dot(B,W)
X = np.linalg.lstsq(Aw, Bw)
相同的值和相同的结果.
Same values and same results.
这篇关于如何在权重矩阵中使用最小二乘法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文