polyfit không hoạt động, nhưng có ít nhất các công cụ tối thiểu hóa hình vuông ở ngoài đó. Tôi muốn giới thiệu kmpfit, có sẵn tại
http://www.astro.rug.nl/software/kapteyn-beta/kmpfittutorial.html
Nó là mạnh mẽ hơn mà polyfit, và có là một ví dụ trên trang của họ trong đó cho thấy làm thế nào để làm một sự phù hợp tuyến tính đơn giản mà nên cung cấp những điều cơ bản làm một trật tự thứ 2 đa thức phù hợp.
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)
Sự thành công của những điều này phụ thuộc chặt chẽ vào các giá trị khởi đầu cho phù hợp, vì vậy chọn một cách cẩn thận nếu có thể. Với rất nhiều thông số miễn phí, nó có thể là một thách thức để có được một giải pháp.
Tôi đã đăng mã ở đây để giải quyết vấn đề này [https://github.com/mrocklin/multipolyfit](https://github.com/mrocklin/multipolyfit) – MRocklin