# Get the Polynomial Equation with Two Variables in Python

TL;DR

predict "price", given "length" and "wandRate"

I have some time-series data where the dependent variable is a polynomial result of 2 independent data points.

Here is a snippet:

This is past pricing data of Processed Rice Grains of a certain kind of rice.

Based on the variable "wandRate" (1st variable) which is the price for any "length" (2nd variable) over 8.2, prices of rice grains with lower lengths are calculated.

These prices are based on a long trial-and-error method of asking various experts on their "opinion" on how a certain grain of a certain length should be priced. There are other variables which can't be objectively measured, but length is the main indicator. I was wondering if it would be possible to create an objective model or find a polynomial equation in two variables to predict "price", given "length" and "wandRate"

I was led to thinking in terms of a polynomial when I plotted the data in google sheets and a sixth-degree-polynomial equation gave an intuitively correct looking trendline.

NOTE: I do not have a strong math background so simple google searches about "polynomial in 2 variables from data python equation" did not yield any implementable results.

I'm looking for some python code to accomplish this. ANY guidance on where to look would be appreciated.

So you want to fit 6-th degree polynomial in python to your data?

Main thing you should note is that it will be still linear regression, its juts that predictors are polynomial (most important is that your weights are still linear (betas in lin.regression))

You can transform your features to polynomial using this sklearn module and then use these features in your linear regression model.

> from sklearn.preprocessing import PolynomialFeatures from sklearn
> import linear_model poly = PolynomialFeatures(degree=6)
> poly_variables = poly.fit_transform(variables_length_wand_rate)
> poly_var_train, poly_var_test, res_train, res_test = train_test_split(poly_variables, results,test_size = 0.3, random_state=4)
> regression = linear_model.LinearRegression()
> model = regression.fit(poly_var_train,res_train)
> score = model.score(poly_var_test, res_test)

• this helped so much! I've managed to save the model, and also find the coefficients and intercept et al. but my main question is still unanswered. I would like the end result to be an equation that I can then use as a mathematical formula in other language (swift) or just excel, without having to load and build a language specific model. It would be great to have a mathematical equation which only needs input parameters and no other fancy libraries. Dec 20 '19 at 15:00

I think numpy might offer quite a good solution called polyfit (see here), but does note that polynomials in higher orders may oscillate significantly.

Another way to fit your polynomial function would be to use an optimiser to determine the coefficients of each polynomial term. You might need to do a bit more coding though.