我有两组数据,我想找到其中的相关性。尽管数据相当分散,但存在明显的关系。我目前使用 numpy polyfit (第 8 阶),但该行有一些“摆动”(特别是在开头和结尾),这是不合适的。其次,我认为线条开始处的拟合效果不是很好(曲线应该稍微陡一些。
如何通过这些数据点获得最适合的“样条线”?
我当前的代码:
# fit regression line
regressionLineOrder = 8
regressionLine = np.polyfit(data['x'], data['y'], regressionLineOrder)
p = np.poly1d(regressionLine)
最佳答案
看看 @MatthewDrury 对 Why use regularisation in polynomial regression instead of lowering the degree? 的回答。这简直太棒了而且恰到好处。最有趣的一点是在最后,当他开始谈论使用自然三次样条来代替 10 次正则化多项式来拟合回归时。您可以使用 scipy.interpolate.CubicSpline
的实现。完成非常相似的事情。 scipy.interpolate
中包含大量其他样条方法的类。类似的方法。
这是一个简单的例子:
from scipy.interpolate import CubicSpline
cs = CubicSpline(data['x'], data['y'])
x_range = np.arange(x_min, x_max, some_step)
plt.plot(x_range, cs(x_range), label='Cubic Spline')
关于python - 通过散点拟合样条曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43611519/