我试着用样条线绘制点+平滑线。但是这条线“过冲”了一些点,例如在下面的代码中,超过了点0.85。
import numpy as np 
import matplotlib.pyplot as plt
from scipy.interpolate import spline
x=np.array([0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9, 2])
y=np.array([0.57,0.85,0.66,0.84,0.59,0.55,0.61,0.76,0.54,0.55,0.48])
x_new = np.linspace(x.min(), x.max(),500)
y_smooth = spline(x, y, x_new)
plt.plot (x_new,y_smooth)
plt.scatter (x, y)我该如何修复它?
发布于 2021-07-05 16:57:40
您也可以尝试使用来自scipy的径向基函数插值的thin plate spline:
import numpy as np 
import matplotlib.pyplot as plt
from scipy.interpolate import Rbf
x = np.array([0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9, 2])
y = np.array([0.57,0.85,0.66,0.84,0.59,0.55,0.61,0.76,0.54,0.55,0.48])
x_new = np.linspace(x.min(), x.max(), 500)
rbf = Rbf(x, y, function = 'thin_plate', smooth = 0.001)
y_smooth = rbf(x_new)
plt.plot(x_new, y_smooth)
plt.scatter (x, y);

通过改变smooth参数可以获得更好的数据近似值。
要考虑的替代function参数值包括“多二次”、“逆”、“高斯”、“线性”、“立方”和“五次”。在考虑立方体的值时,我通常会先尝试'thin_plate‘,然后再尝试’function‘。
检查scipy.interpolate.Rbf docs中的其他径向基函数选项。
https://stackoverflow.com/questions/47361589
复制相似问题