Linear Regression using scipy.ODR fails (Not full rank at solution)

前端 未结 1 483
有刺的猬
有刺的猬 2021-01-26 00:53

so was trying a linear regression with scipy.odr. However, it failed miserably. scipy.odr has worked for me before, and I don\'t see any errors in my code. The only reason I can

相关标签:
1条回答
  • 2021-01-26 01:19
    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    x   = np.linspace(0,1E15,10)
    y   = 1E-15*x-2    
    ax1.set_xlim(-0.05E15,1.1E15)
    ax1.set_ylim(-2.1, -0.7)    
    ax1.plot(x, y, 'o')
    # Fit using odr
    
    
    def f(B, x):
        return B[0]*x + B[1]
    
    sx = np.std(x)
    sy = np.std(y)
    linear = Model(f)
    mydata = RealData(x=x,y=y, sx=sx, sy=sy)
    myodr = ODR(mydata, linear, beta0=[1.00000000e-15, 2.])
    myoutput = myodr.run()
    myoutput.pprint()
    
    a, b = myoutput.beta
    sa, sb = myoutput.sd_beta
    
    xp = np.linspace(min(x), max(x), 1000)
    yp = a*xp+b
    ax1.plot(xp,yp)
    plt.show()
    

    enter image description here

    0 讨论(0)
提交回复
热议问题