Issue
I have several data sets of sample points sharing the same x-coordinates and did a polynomial fit taking all this sample points into account. That works fine as shown in this plot:
using the following code:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
x = np.array([0., 4., 9., 12., 16., 20., 24., 27.])
y = np.array([[3620000.,26000000.,187000000.,348000000.,475000000.,483000000.,456000000.,384000000.],
[3750000.,25900000.,187000000.,362000000.,449000000.,465000000.,488000000.,408000000.],
[3720000.,26100000.,184000000.,341000000.,455000000.,458000000.,446000000.,430000000.]])
x_all = np.ravel(x + np.zeros_like(y))
y_all = np.ravel(y)
plt.scatter(x, y[0], label="training points 1", c='r')
plt.scatter(x, y[1], label="training points 2", c='b')
plt.scatter(x, y[2], label="training points 3", c='g')
x_plot = np.linspace(0, max(x), 100)
for degree in np.arange(5, 6, 1):
model = make_pipeline(PolynomialFeatures(degree), Ridge(alpha=50, fit_intercept=False))
model.fit(x_all[:, None], y_all)
y_plot = model.predict(x_plot[:, None])
plt.plot(x_plot, y_plot, label="degree %d" % degree)
ridge = model.named_steps['ridge']
print(degree, ridge.coef_)
plt.legend(loc='best')
plt.show()
What I am actually interested in is not the equation of the fitted polynomial but actual its derivative.
Is there a way to directly access the derivative of the fitted function? The object model
in the code above has the following attributes:
model.decision_function model.fit_transform model.inverse_transform model.predict model.predict_proba model.set_params model.transform
model.fit model.get_params model.named_steps model.predict_log_proba model.score model.steps
So in the ideal case I would like to have something like (pseudo code):
myDerivative = model.derivative(x_plot)
EDIT:
I am also more than happy to use another module/library that gets the job done, so I am also open for suggestions.
Solution
Since you know the fitted polynomial coefficients will this get what you want?
deriv = np.polyder(ridge.coef_[::-1])
yd_plot = np.polyval(deriv,x_plot)
Answered By - screenpaver
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.