gpt4 book ai didi

python - scipy BSpline 在 python 中的拟合

转载 作者:行者123 更新时间:2023-11-28 20:36:23 25 4
gpt4 key购买 nike

这是我第一次使用 BSpline,我想为我的数据点拟合一条曲线。我试过使用单变量样条曲线并尝试使用 splev 和 splrep,但我真的很想学习如何使用 BSpline 来做到这一点。

看起来我的拟合真的很不稳定,线条甚至没有穿过点。

arraymagU = linspace(U_timeband.min(),U_timeband.max(),300) #array for my x data points
UfunctionBS = BSpline(U_timeband,U_magband,k=4,extrapolate=False)
arraymagU2 = UfunctionBS(arraymagU)

plt.plot(arraymagU,arraymagU2)

U_timeband 是我的 x 坐标,U_magband 只是我的 y。 k=4 我认为表示立方拟合?我试过这个值,但它似乎并没有使它变得更好。

它产生这个:

this

我怎样才能让它变得更好、更一致?我想我可能必须定义断点,但我也不确定该怎么做。

最佳答案

splrep返回一个元组 (t,c,k),其中包含节点向量、B 样条系数和样条的阶数。这些可以被馈送到 interpolate.BSpline 以创建 BSpline 对象:

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

x = np.array([ 0. , 1.2, 1.9, 3.2, 4. , 6.5])
y = np.array([ 0. , 2.3, 3. , 4.3, 2.9, 3.1])

t, c, k = interpolate.splrep(x, y, s=0, k=4)
print('''\
t: {}
c: {}
k: {}
'''.format(t, c, k))
N = 100
xmin, xmax = x.min(), x.max()
xx = np.linspace(xmin, xmax, N)
spline = interpolate.BSpline(t, c, k, extrapolate=False)

plt.plot(x, y, 'bo', label='Original points')
plt.plot(xx, spline(xx), 'r', label='BSpline')
plt.grid()
plt.legend(loc='best')
plt.show()

enter image description here

关于python - scipy BSpline 在 python 中的拟合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45179024/

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