How to use least squares with weight matrix?

后端 未结 3 1504
刺人心
刺人心 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 14:00

    I don't know how you have defined your weights, but you could try this if appropriate:

    import numpy as np
    A=np.array([[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,0,0]])
    B = np.array([1,1,1,1,1])
    W = np.array([1,2,3,4,5])
    Aw = A * np.sqrt(W[:,np.newaxis])
    Bw = B * np.sqrt(W)
    X = np.linalg.lstsq(Aw, Bw)
    

提交回复
热议问题