Skip to content
Advertisement

Which library/function should I use to fit a multivariate polynom to my data?

I have data that depends on 4 independent variables (x1,x2,x3,x4) and I need a model (available in Python) to evaluate f(x1,x2,x3,x4) outside the data points. In principle, if I set 3 of my variables as constant values I can always use a polynomial fit of a reasonable degree (<5) to interpolate the data in the remaining dimension so I would like to generate a function that is capable to interpolate in all dimensions at once using a multivariate polynomial fit. It must be noted that the underlying function is non-linear (meaning that I should expect terms of the form x1^n*x2^m where n,m are not 0). What do you recommend?

To illustrate I am including a small sample of data:

(Note that the fact that some variables appear to be constant is due to the fact that this is just a small sample)

JavaScript

Advertisement

Answer

You can do multivariate curve fitting use the scipy.optimize.curve_fit() function. It is well documented and there are multiple questions and answers on StackOverflow on using it for multivariate fitting.

For your case, something like this can help you start off

JavaScript

A couple of things to note, you need to make sure that the X and y you pass to curve_fit() have the correct dimensions. X must have dimensions of N x M, where N is the number of data points you have, and M is the number of independent variables you have. y should be of length N.

You must also define your fitting function based on the form that you would like, and try and give an initial guess, p0, for the parameters in the function to help curve_fit find the optimal values.

Hope that helps, there are lots of good answers on multivariate fitting with curve_fit() on StackOverflow (see here and here) and the curve_fit documentation should be of help as well.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement