Multivariate polynomial regression with numpy

前端 未结 3 1331
死守一世寂寞
死守一世寂寞 2020-12-02 23:38

I have many samples (y_i, (a_i, b_i, c_i)) where y is presumed to vary as a polynomial in a,b,c up to a certain degree. For example f

3条回答
  •  执笔经年
    2020-12-02 23:52

    polyfit does work, but there are better least square minimizers out there. I would recommend kmpfit, available at

    http://www.astro.rug.nl/software/kapteyn-beta/kmpfittutorial.html

    It is more robust that polyfit, and there is an example on their page which shows how to do a simple linear fit that should provide the basics of doing a 2nd order polynomial fit.

    
    def model(p, v, x, w):       
       a,b,c,d,e,f,g,h,i,j,k = p      #coefficients to the polynomials      
       return  a*v**2 + b*x**2 + c*w**2 + d*v*x + e*v*w + f*x*w + g*v + h*x + i*y + k  
    
    def residuals(p, data):        # Function needed by fit routine
       v, x, w, z = data            # The values for v, x, w and the measured hypersurface z
       a,b,c,d,e,f,g,h,i,j,k = p   #coefficients to the polynomials  
       return (z-model(p,v,x,w))   # Returns an array of residuals. 
                                   #This should (z-model(p,v,x,w))/err if 
                                   # there are error bars on the measured z values
    
    
    #initial guess at parameters. Avoid using 0.0 as initial guess
    par0 = [1.0, 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0] 
    
    #create a fitting object. data should be in the form 
    #that the functions above are looking for, i.e. a Nx4 
    #list of lists/tuples like (v,x,w,z) 
    fitobj = kmpfit.Fitter(residuals=residuals, data=data)
    
    # call the fitter 
    fitobj.fit(params0=par0)
    

    The success of these things is closely dependent on the starting values for the fit, so chose carefully if possible. With so many free parameters it could be a challenge to get a solution.

提交回复
热议问题