I have been trying to use the scikit-learn library to solve this problem. Roughly:
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
# Make or load an n x p data matrix X and n x 1 array y of the corresponding
# function values.
poly = PolynomialFeatures(degree=2)
Xp = poly.fit_transform(X)
model = LinearRegression()
model.fit(Xp, y)
# Approximate the derivatives of the gradient and Hessian using the relevant
# finite-difference equations and model.predict.
As the above illustrates, sklearn
makes the design choice to separate polynomial regression into PolynomialFeatures
and LinearRegression
rather than combine these into a single function. This separation has conceptual advantages but also a major drawback: it effectively prevents model
from offering the methods gradient
and hessian
, and model
would be significantly more useful if it did.
My current work-around uses finite-difference equations and model.predict
to approximate the elements of the gradient and Hessian (as described here). But I don't love this approach — it is sensitive to floating-point error and the "exact" information needed to build the gradient and Hessian is already contained in model.coef_
.
Is there any more elegant or accurate method to fit a p-dimensional polynomial and find its gradient and Hessian within Python? I would be fine with one that uses a different library.
poly_eq
does not reflect the polynomial features created using sklearn. In particular, it assumes that in these features each variable appears with exponent either 0 or 1, which is not the case. Once this is fixed, it should work. – Leap