gpt4 book ai didi

python - 使用 scipy.interpolate.splrep 函数

转载 作者:太空狗 更新时间:2023-10-29 20:59:05 32 4
gpt4 key购买 nike

我正在尝试将三次样条拟合到一组给定的点。我的积分没有排序。我无法对这些点进行排序或重新排序,因为我需要该信息。

但由于函数 scipy.interpolate.splrep 仅适用于非重复和单调递增的点,我定义了一个将 x 坐标映射到单调递增空间的函数。

我的旧观点是:

xpoints=[4913.0, 4912.0, 4914.0, 4913.0, 4913.0, 4913.0, 4914.0, 4915.0, 4918.0, 4921.0, 4925.0, 4932.0, 4938.0, 4945.0, 4950.0, 4954.0, 4955.0, 4957.0, 4956.0, 4953.0, 4949.0, 4943.0, 4933.0, 4921.0, 4911.0, 4898.0, 4886.0, 4874.0, 4865.0, 4858.0, 4853.0, 4849.0, 4848.0, 4849.0, 4851.0, 4858.0, 4864.0, 4869.0, 4877.0, 4884.0, 4893.0, 4903.0, 4913.0, 4923.0, 4935.0, 4947.0, 4959.0, 4970.0, 4981.0, 4991.0, 5000.0, 5005.0, 5010.0, 5015.0, 5019.0, 5020.0, 5021.0, 5023.0, 5025.0, 5027.0, 5027.0, 5028.0, 5028.0, 5030.0, 5031.0, 5033.0, 5035.0, 5037.0, 5040.0, 5043.0]

ypoints=[10557.0, 10563.0, 10567.0, 10571.0, 10575.0, 10577.0, 10578.0, 10581.0, 10582.0, 10582.0, 10582.0, 10581.0, 10578.0, 10576.0, 10572.0, 10567.0, 10560.0, 10550.0, 10541.0, 10531.0, 10520.0, 10511.0, 10503.0, 10496.0, 10490.0, 10487.0, 10488.0, 10488.0, 10490.0, 10495.0, 10504.0, 10513.0, 10523.0, 10533.0, 10542.0, 10550.0, 10556.0, 10559.0, 10560.0, 10559.0, 10555.0, 10550.0, 10543.0, 10533.0, 10522.0, 10514.0, 10505.0, 10496.0, 10490.0, 10486.0, 10482.0, 10481.0, 10482.0, 10486.0, 10491.0, 10497.0, 10506.0, 10516.0, 10524.0, 10534.0, 10544.0, 10552.0, 10558.0, 10564.0, 10569.0, 10573.0, 10576.0, 10578.0, 10581.0, 10582.0]

情节:

Erroneous trace映射函数和插值的代码是:

xnew=[]
ynew=ypoints

for c3,i in enumerate(xpoints):
if np.isfinite(np.log(i*pow(2,c3))):
xnew.append(np.log(i*pow(2,c3)))
else:
if c==0:
xnew.append(np.random.random_sample())
else:
xnew.append(xnew[c3-1]+np.random.random_sample())
xnew=np.asarray(xnew)
ynew=np.asarray(ynew)
constant1=10.0
nknots=len(xnew)/constant1
idx_knots = (np.arange(1,len(xnew)-1,(len(xnew)-2)/np.double(nknots))).astype('int')
knots = [xnew[i] for i in idx_knots]
knots = np.asarray(knots)
int_range=np.linspace(min(xnew),max(xnew),len(xnew))
tck = interpolate.splrep(xnew,ynew,k=3,task=-1,t=knots)
y1= interpolate.splev(int_range,tck,der=0)

代码在函数 interpolate.splrep() 中针对上面的一些点集抛出错误。

错误是:文件“/home/neeraj/Desktop/koustav/res/BOS5/fit_spline3.py”,第 58 行,在 save_spline_f 中tck = interpolate.splrep(xnew,ynew,k=3,task=-1,t=knots)splrep 中的文件“/usr/lib/python2.7/dist-packages/scipy/interpolate/fitpack.py”,第 465 行提高_iermess ier (_iermess[ier][0])ValueError:输入数据错误

但对于其他点集,它工作正常。例如对于以下一组点。

xpoints=[1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1629.0, 1630.0, 1630.0, 1630.0, 1631.0, 1631.0, 1631.0, 1631.0, 1630.0, 1629.0, 1629.0, 1629.0, 1628.0, 1627.0, 1627.0, 1625.0, 1624.0, 1624.0, 1623.0, 1620.0, 1618.0, 1617.0, 1616.0, 1615.0, 1614.0, 1614.0, 1612.0, 1612.0, 1612.0, 1611.0, 1610.0, 1609.0, 1608.0, 1607.0, 1607.0, 1603.0, 1602.0, 1602.0, 1601.0, 1601.0, 1600.0, 1599.0, 1598.0]

ypoints=[10570.0, 10572.0, 10572.0, 10573.0, 10572.0, 10572.0, 10571.0, 10570.0, 10569.0, 10565.0, 10564.0, 10563.0, 10562.0, 10560.0, 10558.0, 10556.0, 10554.0, 10551.0, 10548.0, 10547.0, 10544.0, 10542.0, 10541.0, 10538.0, 10534.0, 10532.0, 10531.0, 10528.0, 10525.0, 10522.0, 10519.0, 10517.0, 10516.0, 10512.0, 10509.0, 10509.0, 10507.0, 10504.0, 10502.0, 10500.0, 10501.0, 10499.0, 10498.0, 10496.0, 10491.0, 10492.0, 10488.0, 10488.0, 10488.0, 10486.0, 10486.0, 10485.0, 10485.0, 10486.0, 10483.0, 10483.0, 10482.0, 10480.0]

情节: Trace for which there was no error任何人都可以建议发生了什么事吗?提前致谢......

最佳答案

实际上您不必自己定义新函数。是这样的非常多的轨迹插值 :scipy: Interpolating trajectory( scipy: Interpolating trajectory )

这个答案对我来说很好,希望对你有帮助。

from scipy import interpolate as itp
mytck,myu=itp.splprep([xpoints,ypoints])
xnew,ynew= itp.splev(np.linspace(0,1,1000),mytck)
plot(xnew,ynew)

Result after Spline

关于python - 使用 scipy.interpolate.splrep 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17348214/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com