Daily Archives: December 5, 2016

Polynomial Linear Regression by Scikit

Suppose we have a 2-degree polynomial function polynomial1

And let’s generate some training data sets. First, let’s have some random (x1, x2) sets:
polynomial2

Then, we transform it to (1, x1, x2, x1x2, x1^2, x2^2) form:
polynomial3

For polynomial1, we know the coefficients is [2, 1, -3, 2, -5, 6]. Then we will have training result:
polynomial4

Let’s make it a little randomly, and we have training result set:
polynomial5

So now our training set is:
polynomial6

Now, we can solve Xtrans, y as how we solve multivariate linear regression:

regr = linear_model.LinearRegression()
regr.fit(X_trans, y)
print regr.coef_    # slope
print regr.intercept_   # intercept

And we got the result, it is very similar to the coefficient we set [2, 1, -3, 2, -5, 6]
polynomial7

Check my code on github.