【发布时间】:2021-01-30 13:04:11
【问题描述】:
我想计算拟合 GAM 模型 a 在 pyGAM 中特定点的导数。该模型仅包含样条项,因此它实际上应该是一个 BSpline。
确实,使用pygam.utils.b_spline_basis 给出了一个矩阵,我可以将其乘以a.coefs 以从a.predict 恢复pyGAM 预测。
这个想法是使用 BSpline 的解析导数来获得导数。有关详细信息,请参阅 here 或直接查看 Wikipedia 的 BSpline 文章。
我想出了这个:
def GetGAM(x,y,err):
if len(x)>5:
gam=pygam.LinearGAM(pygam.s(0, n_splines=len(x)),penalties=__PenaltyD3,lam=0,fit_intercept=False)
gam=gam.fit(x,y, weights=1/err)
#print('done')
return gam
x1 = np.linspace(0,50,10)
y1=x1**2
err=np.ones(10)
a = GetGAM(x1,y1,err)
x0=pygam.utils.b_spline_basis(x1,pygam.utils.gen_edge_knots(x1,'numerical'),spline_order=3,n_splines=len(a.coef_), periodic=False)
x=pygam.utils.b_spline_basis(x1,pygam.utils.gen_edge_knots(x1[:-1],'numerical'),spline_order=2,n_splines=len(a.coef_), periodic=False)
恢复y1:x0@a.coefs_工作得很好。
为了得到我的导数
der2=x[:,1:]@(a.coef_[1:]-a.coef_[:-1])/((edges[1]-edges[0])/(len(a.coef_)))
我也尝试了各种变体,但无法得到令人满意的解决方案 (2*x1)。
【问题讨论】:
标签: curve-fitting spline gam bspline pygam