How to use least squares with weight matrix?

后端 未结 3 1503
刺人心
刺人心 2021-02-03 13:08

I know how to solve A.X = B by least squares using Python:

Example:

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.ls         


        
3条回答
  •  一生所求
    2021-02-03 13:36

    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.

提交回复
热议问题