【问题标题】:Fit spline through scatter通过散点拟合样条
【发布时间】:2017-04-25 13:04:42
【问题描述】:

我有两组数据,我想从中找到相关性。尽管有相当多的数据分散,但有明显的关系。我目前使用 numpy polyfit (第 8 阶),但是该行有一些“摆动”(尤其是在开头和结尾处),这是不合适的。其次,我认为直线开头的拟合不是很好(曲线应该稍微陡峭。

如何通过这些数据点获得最适合的“样条”?

我当前的代码:

# fit regression line
regressionLineOrder = 8
regressionLine = np.polyfit(data['x'], data['y'], regressionLineOrder)
p = np.poly1d(regressionLine)

【问题讨论】:

  • 这可能更像是Cross Validated,但无论如何,这些效果对于多项式拟合来说是自然的。如果您想要更好的曲线,您可能需要使用更高级的回归技术; scikit-learn 提供了几种算法。 Gaussian processes 在这里可能是一个不错的选择,尽管直接使用它可能数据太多。

标签: python numpy scipy non-linear-regression


【解决方案1】:

看看@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')

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多