[英]How to extract derivative from a polynomial fit?
我有幾個共享相同 x 坐標的樣本點數據集,並考慮了所有這些樣本點進行了多項式擬合。 這很好用,如下圖所示:
使用以下代碼:
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()
我真正感興趣的不是擬合多項式的方程,而是它的實際導數。
有沒有辦法直接訪問擬合函數的導數? 上面代碼中的對象model
具有以下屬性:
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
所以在理想的情況下,我想要類似的東西(偽代碼):
myDerivative = model.derivative(x_plot)
編輯:
我也很高興使用另一個可以完成工作的模塊/庫,所以我也願意接受建議。
既然你知道擬合多項式系數,這會得到你想要的嗎?
deriv = np.polyder(ridge.coef_[::-1])
yd_plot = np.polyval(deriv,x_plot)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.