且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Python中将曲线拟合到数据集时

更新时间:2022-03-17 03:49:10

如果我很好理解,您的问题将是一个概念性问题,而不是实用性问题.

If I understand well, your question is much rather a conceptual than a practical one.

如果要显示一条稍微代表您的数据集的线,可以从三件事开始:移动平均,插值和多项式拟合.

If you want to show a line that somewhat represents your dataset, you could start with three things: moving average, interpolation and polynomial fit.

移动平均可以很好地平滑您的数据集.我不知道它的内置功能,但是您可以自己编写它,因为它已经在这里.

Moving average smoothes your dataset nicely. I'm not aware of a built-in function for it, but you can code it yourself, as it was discussed here.

插值(样条线(例如)适合您的数据集上的某些函数,可以在许多点对其进行评估然后进行绘制.

Interpolation (spline, for example) fits some function on your dataset which can be evaluated at many points and then plotted.

使用上述两种方法,您不必指定函数.但是,您可以自己拟合多项式函数.如何确定多项式的次数?您可以获取所有数据点的对数,将线性线拟合到对数数据中,如果拟合良好,则可以将线性部分的系数视为多项式对原始数据集的次数.但是,不要使用过多的多项式-您可以轻松地为此过度拟合方法.

With the two mentioned methods, you don't have to specify a function. However, you can fit a polynomial function yourself. How to determine the degree of the polynomial? You can take the log of all your data points, fit a linear line to the log data, and IF IT FITS WELL, the coefficient of the linear part can be considered as the degree of the polynomial to the original dataset. However, don't use too large degree of polynomials - you can easily run into overfitting with this method.