How to derive equation from Numpy's polyfit?

后端 未结 2 1955
醉酒成梦
醉酒成梦 2020-12-18 14:47

Given an array of x and y values, the following code will calculate a regression curve for these data points.

# calculate polynomial
z = np.polyfit(x, y, 5)
         


        
相关标签:
2条回答
  • 2020-12-18 15:08

    If you want to show the equation, you can use sympy to output latex:

    from sympy import S, symbols, printing
    from matplotlib import pyplot as plt
    import numpy as np
    
    x=np.linspace(0,1,100)
    y=np.sin(2 * np.pi * x)
    
    p = np.polyfit(x, y, 5)
    f = np.poly1d(p)
    
    # calculate new x's and y's
    x_new = np.linspace(x[0], x[-1], 50)
    y_new = f(x_new)
    
    x = symbols("x")
    poly = sum(S("{:6.2f}".format(v))*x**i for i, v in enumerate(p[::-1]))
    eq_latex = printing.latex(poly)
    
    plt.plot(x_new, y_new, label="${}$".format(eq_latex))
    plt.legend(fontsize="small")
    plt.show()
    

    the result:

    0 讨论(0)
  • 2020-12-18 15:08

    Construct a simple example:

    In [94]: x=np.linspace(0,1,100)
    In [95]: y=2*x**3-3*x**2+x-1
    
    In [96]: z=np.polyfit(x,y,3)
    In [97]: z
    Out[97]: array([ 2., -3.,  1., -1.])
    

    The z coefficients correspond to the [2,-3,1,-1] I used to construct y.

    In [98]: f=np.poly1d(z)
    In [99]: f
    Out[99]: poly1d([ 2., -3.,  1., -1.])
    

    The str, or print, string for f is a representation of this polynomial equation. But it's the z coeff that defines the equation.

    In [100]: print(f)
       3     2
    2 x - 3 x + 1 x - 1
    In [101]: str(f)
    Out[101]: '   3     2\n2 x - 3 x + 1 x - 1'
    

    What else do you mean by 'actual equation'?

    polyval will evaluate f at a specific set of x. Thus to recreate y, use polyval(f,x):

    In [107]: np.allclose(np.polyval(f,x),y)
    Out[107]: True
    
    0 讨论(0)
提交回复
热议问题