Find uncertainty from polyfit

前端 未结 2 677
青春惊慌失措
青春惊慌失措 2021-02-03 13:56

I use simple polyfit of order 2 to fit a line in sample data:

np.polyfit(x, y, 2)

which returns the coefficients.

Now I wa

2条回答
  •  故里飘歌
    2021-02-03 14:42

    For your convenience I made a fully working example for Python 3 based on gg349's answer.

    import numpy as np
    import matplotlib.pyplot as plt 
    
    x = np.linspace(0,1,1000)
    # comment and uncomment the last term to see how the fit appears in the figure,
    # and how the covariances of the single polynomial coefficients vary in turn.
    y = np.cos(x) * x**2 + x + np.sin(x - 1.) \
    #     + (x * 1.3)**6
    
    p, cov = np.polyfit(x, y, 2, cov=True)
    
    plt.plot(x, y)
    plt.plot(x, np.polyval(p,x))
    plt.show()
    
    print(np.sqrt(np.diag(cov)))
    

提交回复
热议问题