How to use least squares with weight matrix?

后端 未结 3 1515
刺人心
刺人心 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:52

    scikit package offers weighted regression directly .. https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression.fit

    import numpy as np
    # generate random data
    N = 25
    xp = [-5.0, 5.0]
    x = np.random.uniform(xp[0],xp[1],(N,1))
    e = 2*np.random.randn(N,1)
    y = 2*x+e
    w = np.ones(N)
    
    # make the 3rd one outlier
    y[2] += 30.0
    w[2] = 0.0
    
    from sklearn.linear_model import LinearRegression
    # fit WLS using sample_weights
    WLS = LinearRegression()
    WLS.fit(x, y, sample_weight=w)
    
    from matplotlib import pyplot as plt
    plt.plot(x,y, '.')
    plt.plot(xp, xp*WLS.coef_[0])
    plt.show()
    

提交回复
热议问题