python– 在样条拟合1d数据中找到拐点

python– 在样条拟合1d数据中找到拐点,第1张

概述我有一些一维数据,并与样条拟合.然后我想在其中找到拐点(忽略鞍点).现在我通过在splev生成的很多值上使用scipy.signal.argrelmin(和argrelmax)来搜索其第一个派生的极值.import scipy.interpolate import scipy.optimize import scipy.signal import numpy

我有一些一维数据,并与样条拟合.然后我想在其中找到拐点(忽略鞍点).现在我通过在splev生成的很多值上使用scipy.signal.argrelmin(和argrelmax)来搜索其第一个派生的极值.

import scipy.interpolateimport scipy.optimizeimport scipy.signalimport numpy as npimport matplotlib.pyplot as pltimport operatory = [-1,5,6,4,2,8,1]x = np.arange(0,len(y))tck = scipy.interpolate.splrep(x,y,s=0)print 'roots',scipy.interpolate.sproot(tck)# output:# [0.11381478]xnew = np.arange(0,len(y),0.01)ynew = scipy.interpolate.splev(xnew,tck,der=0)ynew_deriv = scipy.interpolate.splev(xnew,der=1)min_IDxs = scipy.signal.argrelmin(ynew_deriv)max_IDxs = scipy.signal.argrelmax(ynew_deriv)mins = zip(xnew[min_IDxs].toList(),ynew_deriv[min_IDxs].toList())maxs = zip(xnew[max_IDxs].toList(),ynew_deriv[max_IDxs].toList())inflection_points = sorted(mins + maxs,key=operator.itemgetter(0))print 'inflection_points',inflection_points# output:# [(3.13,-2.9822449358974357),#  (5.03,4.3817785256410255)#  (7.13,-4.867132628205128)]plt.legend(['data','Cubic Spline','1st deriv'])plt.plot(x,'o',xnew,ynew,'-',ynew_deriv,'-')plt.show()

但这感觉非常错误.我想有可能在没有产生这么多价值的情况下找到我要找的东西.像sproot这样的东西,但也许适用于二次推导?最佳答案因此,您可以首先在样条拟合样条,然后使用导数公式构造导数样条的系数,最后使用样条根寻找得到导数样条的根.这些是原始曲线的最大值/最小值.

这是代码:https://gist.github.com/pv/5504366

系数的相关计算是:

t,c,k = scipys_spline_representation# Compute the denominator in the differentiation formula.dt = t[k+1:-1] - t[1:-k-1]# Compute the new coefficIEntsd = (c[1:-1-k] - c[:-2-k]) * k / dt# Adjust knotst2 = t[1:-1]# Pad coefficIEnt array to same size as knots (FITPACK convention)d = np.r_[d,[0]*k]# Done,a new splinenew_spline_repr = t2,d,k-1
总结

以上是内存溢出为你收集整理的python – 在样条拟合1d数据中找到拐点全部内容,希望文章能够帮你解决python – 在样条拟合1d数据中找到拐点所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1205334.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存