简体   繁体   English

如何使用 scipy curve_fit 将曲线拟合到此数据

[英]How to fit a curve to this data using scipy curve_fit

I am hoping someone can me with where I'm going wrong with fitting a curve to this data.我希望有人可以解决我在为这些数据拟合曲线时出错的地方。 I am using the method in this link and so have the following code:我正在使用此链接中的方法,因此具有以下代码:

def sigmoid(x, L, x0, k, b):
    y = L / (1 + np.exp(-k*(x-x0)))+b
    return y

p0 = [max(y1), np.median(x2), 1, min(y1)]

popt, pcov = curve_fit(sigmoid, xdata=x2, ydata=y1, p0=p0, method='dogbox')

predictions = sigmoid(x2, *popt)

And my plotted "curve" looks like so:我绘制的“曲线”看起来像这样:

截屏

But I am expecting a more s-shaped curve.但我期待一个更 S 形的曲线。 I have experimented with different p0 values but not getting the required output (and if I'm honest I'm not sure how I'm supposed to find the ideal starting parameters).我已经尝试了不同的 p0 值,但没有得到所需的输出(老实说,我不确定我应该如何找到理想的起始参数)。

Using p0 = [max(y1), np.median(x2), 0.4, 1] and method='trf I did get the following, which is closer but still missing the curve in the middle?使用p0 = [max(y1), np.median(x2), 0.4, 1]method='trf我确实得到了以下结果,哪个更接近但仍然缺少中间的曲线?

在此处输入图片说明

Any help greatly appreciated!非常感谢任何帮助!

That is because your y-axis is a log scale.那是因为您的 y 轴是对数刻度。 If you change the y-axis to a linear one, you'll see that the fit is actually quite good.如果您将 y 轴更改为线性轴,您会发现拟合实际上非常好。

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

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