Issue
Problem context
Using scikit-learn
with Python, I'm trying to fit a quadratic polynomial curve to a set of data, so that the model would be of the form y = a2x^2 + a1x + a0
and the an
coefficients will be provided by a model.
The problem
I don't know how to fit a polynomial curve using that package and there seem to be surprisingly few, clear references on how to do it (I've looked for a while). I've seen this question on doing something similar with NumPy, and also this question which does a more complicated fit than I require.
What a good solution would look like
Hopefully, a good solution would go around like this (sample adapted from linear fit code that I'm using):
x = my_x_data.reshape(len(profile), 1)
y = my_y_data.reshape(len(profile), 1)
regression = linear_model.LinearRegression(degree=2) # or PolynomialRegression(degree=2) or QuadraticRegression()
regression.fit(x, y)
I would imagine scikit-learn
would have a facility like this, since it's pretty common (for example, in R
, the formula for fitting can be provided in-code, and they should be able to be pretty interchangeable for that kind of use-case).
The question:
What is a good way to do this, or where can I find information about how to do this properly?
Solution
Possible duplicate: https://stats.stackexchange.com/questions/58739/polynomial-regression-using-scikit-learn.
Is it crucial for some reason that this be done using scikit-learn? The operation you want can be performed very easily using numpy:
z = np.poly1d(np.polyfit(x,y,2))
After which z(x)
returns the value of the fit at x
.
A scikit-learn solution would almost certainly be simply a wrapper around the same code.
Answered By - AGML
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.