简体   繁体   English

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

[英]On fitting a curved line to a dataset in Python

I have a plot with two data sets which produces a slight gradient, where a curved line of best fit may be overplotted. 我有一个带有两个数据集的图,它产生一个轻微的梯度,其中最佳拟合曲线可能是过度绘制的。

At the moment I have only managed to get a straight line of best fit. 目前我只是设法得到最合适的直线。 I understand scipy.optimize.curve_fit should be able to help me, but this requires me to know the function I want to overplot (I think). 我理解scipy.optimize.curve_fit应该可以帮助我,但这需要我知道我想要scipy.optimize.curve_fit的功能(我认为)。

Below are my code and plots. 以下是我的代码和图表。 How would one go about creating a curved plot for these data sets? 如何为这些数据集创建曲线图?

plt.figure(figsize=(15,6.6))
pl.subplot(1,2,1) 
plt.plot(gg,AA, 'kx')
plt.xlabel('x')
plt.ylabel('y')
plt.gca().invert_yaxis()
y=AA
x=gg
fit=pl.polyfit(x,y,1)
#slope, fit_fn=pl.poly1d(fit)
fit_fn=pl.poly1d(fit)
scat=pl.plot(x,y, 'kx', x,fit_fn(x), '-b' )


pl.subplot(1,2,2) 
pl.plot(LL,pp, 'kx')#shows points with no removal or bestfit
plt.gca().invert_yaxis()

plt.savefig('1.jpg')
plt.show()

我的情节左边的那条线条最合适的直线

It should be noted that there is possibly no curve but I want to discover if there is one which would fit. 应该注意的是,可能没有曲线,但我想发现是否有适合的曲线。

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. 您可以记录所有数据点,将线性线拟合到日志数据,并且如果IT FIT WELL,则可以将线性部分的系数视为原始数据集的多项式的次数。 However, don't use too large degree of polynomials - you can easily run into overfitting with this method. 但是,不要使用太大程度的多项式 - 您可以轻松地使用此方法进行过度拟合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM