gpt4 book ai didi

python - scipy UnivariateSpline 因多值 X 而失败

转载 作者:行者123 更新时间:2023-12-04 11:54:27 27 4
gpt4 key购买 nike

scipy UnivariateSpline 不允许多值 X。我读到这已更改,但似乎对我不起作用。我用的是最新版本,刚刚用pip尝试下载,说我有最新版本。

我曾尝试将 s(平滑)从 0 和 None(定义为 X 必须严格增加)更改,但这并不能解决问题。

import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline

x=[152,152,152,152,152,159,159,159,159,159,166,166,166,166,166,174,174,174,174,174,181,181,181,181,181,188,188,188,188,188,194,194,194,194,194,202,202,202,202,202,208,208,208,208,208,215,215,215,215,215,222,222,222,222,222,229,229,229,229,229,236,236,236,236,236,243,243,243,243,243,250,250,250,250,250,258,258,258,258]

y=[-1.31639523,-1.90045889,-1.81769285,-1.25702203,-1.31975784,-0.76206863,-0.74170737,-0.66029284,-0.58124809,-0.49593701,-0.19309943,0.02254396,-0.04614866,0.06709774,0.10436002,0.577175,0.56809403,0.89547559,0.60922195,0.76220672,1.0461253,1.1304339,1.56360338,1.34189828,1.41658105,1.98677786,2.40487089,2.20431052,1.91072699,2.49328809,2.670556,2.85024397,3.24333426,2.44841554,3.14604703,3.39128172,3.78063788,3.21446612,3.07158159,3.79503965,3.40717945,4.02417242,3.70708767,4.00729682,4.25504517,4.28874564,3.9356614,4.30337567,4.02388633,4.65376986,4.33884509,4.68839858,4.10508666,4.26236997,4.53098529,5.03443645,4.07940011,4.3033351,4.43476139,4.80221614,4.49558967,4.5052504,4.40289487,5.15433152,5.1330299,4.30299696,4.47974301,5.34886789,4.60896298,5.35997675,4.40204983,5.50162549,4.3056854,4.87120463,5.36265274,4.33578634,5.06347439,4.46811258,5.30920785]

s = 0.1 # set smoothing to non-zero
spl = UnivariateSpline(x, y, s=s)

我收到此 错误消息 : -
spl = UnivariateSpline(x, y, s=s)
File "C:\Python37\lib\site-packages\scipy\interpolate\fitpack2.py", line 177, in __init__
raise ValueError('x must be strictly increasing')
ValueError: x must be strictly increasing.

任何帮助或建议将是最受欢迎的!

最佳答案

知道了 !
在查看代码数小时后,我发现了一个链接 https://github.com/kawache/Python-B-spline-examples 给了我一个线索,现在我可以产生与我的原始 FORTRAN 代码(使用 MG Cox 原始代码 1 编写,随后修改为 Cox & de Boor 代码)相似的结果2)。

也许我应该把它写成“在连续的不规则时间点对多次观测的数据进行样条拟合”。

感谢您对此提供的所有帮助。

这是我的代码:-

import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np

x=[152.0,152,152,152,152,159,159,159,159,159,166,166,166,166,166,174,174,174,174,174,181,181,181,181,181,188,188,188,188,188,194,194,194,194,194,202,202,202,202,202,208,208,208,208,208,215,215,215,215,215,222,222,222,222,222,229,229,229,229,229,236,236,236,236,236,243,243,243,243,243,250,250,250,250,250,258,258,258,258]

y=[-1.31639523,-1.90045889,-1.81769285,-1.25702203,-1.31975784,-0.76206863,-0.74170737,-0.66029284,-0.58124809,-0.49593701,-0.19309943,0.02254396,-0.04614866,0.06709774,0.10436002,0.577175,0.56809403,0.89547559,0.60922195,0.76220672,1.0461253,1.1304339,1.56360338,1.34189828,1.41658105,1.98677786,2.40487089,2.20431052,1.91072699,2.49328809,2.670556,2.85024397,3.24333426,2.44841554,3.14604703,3.39128172,3.78063788,3.21446612,3.07158159,3.79503965,3.40717945,4.02417242,3.70708767,4.00729682,4.25504517,4.28874564,3.9356614,4.30337567,4.02388633,4.65376986,4.33884509,4.68839858,4.10508666,4.26236997,4.53098529,5.03443645,4.07940011,4.3033351,4.43476139,4.80221614,4.49558967,4.5052504,4.40289487,5.15433152,5.1330299,4.30299696,4.47974301,5.34886789,4.60896298,5.35997675,4.40204983,5.50162549,4.3056854,4.87120463,5.36265274,4.33578634,5.06347439,4.46811258,5.30920785]

plt.plot(x, y, 'ro', ms=5)

tck,u = interpolate.splprep([x,y],k=3,s=32)

u=np.linspace(0,1,num=50,endpoint=True)
out = interpolate.splev(u,tck)

plt.plot(x, y, 'ro', out[0], out[1], 'b' )

plt.show()

和结果(后跟来自 Cox 1 和 de Boor 2 的原始样条曲线图)。

1 :M. G. Cox,“b 样条的数值评估”,J. Inst.数学应用,10,第 134-149 页,1972 年。

2 :C. de Boor,“使用 b 样条计算”,J. Approximation Theory, 6, p.50-62, 1972。
Plot of results from python code.
Plot of reults from original Fortran code from Cox & De Boor

关于python - scipy UnivariateSpline 因多值 X 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57636197/

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